Fatal error: điện thoại tư vấn to a member function bind_param() on a non-object in...
Bạn đang xem: Hàm mysqli_fetch_assoc trong php
Here is the code:
global $mysqli;$stmt = $mysqli->prepare("SELECT id, description FROM tbl_page_answer_category WHERE cur_own_id = ?");$stmt->bind_param("i", $cur_id);$stmt->execute();$stmt->bind_result($uid, $desc);To kiểm tra my query, I tried lớn execute the query via control panel phpMyAdmin & the result is OK.
php mysqli prepared-statement environment error-reporting
edited Dec 30 "19 at 20:39
Your Common Sense
154k3636 gold badges198198 silver badges321321 bronze badges
asked Mar 26 "14 at 13:27
1,54322 gold badges1111 silver badges77 bronze badges
add a phản hồi |
1 Answer 1
Active Oldest Votes
Sometimes your MySQLi code produces an error like mysqli_fetch_assoc() expects parameter..., điện thoại tư vấn to a thành viên function bind_param()... Or similar. Or even without any error, but the query doesn"t work all the same. It means that your query failed to execute.
Every time a query fails, MySQL has an error message that explains the reason. Unfortunately, by default such errors are not transferred to PHP, & all you"ve got is a cryptic error message mentioned above. Hence it is very important to lớn configure PHP & MySQLi to report MySQL errors to you. And once you get the error message, fixing it will be a piece of cake.
How to get the error message in MySQLi?
First of all, always have this line before MySQLi connect in all your environments:
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);After that all MySQL errors will be transferred into PHP exceptions. Uncaught exception, in turn, makes a PHP fatal error. Thus, in case of a MySQL error, you"ll get a conventional PHP error. That will instantly make you aware of the error cause. & a stack trace will lead you lớn the exact spot where the error occurred.
How to configure PHP in different environments
Here is a gist of my article on PHP error reporting:Reporting errors on a development & live servers must be different. On a development vps it is convenient to have errors shown on-screen, but on a live vps error messages must be logged instead, so you could find them in the error log later.
Therefore, you must phối corresponding configuration options to lớn the following values:
On a development servererror_reporting should be set khổng lồ E_ALL value;log_errors should be set lớn 1 (it is convenient to lớn have logs on a development PC too)display_errors should be set lớn 1
On a production servererror_reporting should be set khổng lồ E_ALL value;log_errors should be set to 1display_errors should be set lớn 0
How to lớn actually use it?
Just remove any code that checks for the error manually, all those or die(), if ($result) và such. Simply write your database interaction code right away:
$stmt = $this->con->prepare("INSERT INTO table(name, quantity) VALUES (?,?)");$stmt->bind_param("si", $name, $quantity);$stmt->execute();again, without any conditions around. If an error occurs, it will be treated as any other error in your code. For example, on a development PC it will just appear on-screen, while on a live site it will be logged for a programmer, whereas for the user"s convenience you could use an error handler (but that"s a different story which is off topic for MySQLi, but you may read about it in the article linked above).
What to vì chưng with the error message you get?
First of all you have khổng lồ locate the problem query. The error message contains the tệp tin name và the line number of the exact spot where the error occurred. For the simple code that"s enough, but if your code is using functions or classes you may need khổng lồ follow the stack trace to lớn locate the problem query.
After getting the error message, you have lớn read & comprehend it. It sounds too obvious if not condescending, but learners often overlook the fact that the error message is not just an alarm signal, but it actually contains a detailed explanation of the problem. & all you need is lớn read the error message and fix the issue.Say, if it says that a particular table doesn"t exist, you have to kiểm tra spelling, typos, letter case. Also you have lớn make sure that your PHP script connects to lớn a correct databaseOr, if it says there is an error in the SQL syntax, then you have khổng lồ examine your SQL. And the problem spot is right before the query part cited in the error message.
If you don"t understand the error message, try to Google it. Và when browsing the results, stick lớn answers that explain the error rather than bluntly give the solution. A solution may not work in your particular case but the explanation will help you lớn understand the problem và make you able khổng lồ fix the issue by yourself.
You have to also trust the error message. If it says that number of tokens doesn"t match the number of bound variables then it is so. The same goes for the absent tables or columns. Given the choice, whether it"s your own mistake or the error message is wrong, always stick to lớn the former. Again it sounds condescending, but hundreds of questions on this very site prove this advise extremely useful.
A list of things you should never ever do in regard of error reportingNever use an error suppression operator (
)! It makes a programmer unable read the error message và therefore unable to fix the errorDo not use die() or echo or any other function lớn print the error message on the screen unconditionally. PHP can report errors by itself & do it the right way depends on the environment - so just leave it for PHP.Do not showroom a condition to demo the query result manually (like if($result)). With error exceptions enabled such condition will just be useless.Do not use try..catch operator for echoing the error message. This operator should be used khổng lồ perform some error handling, like a transaction rollback. But never use it just lớn report errors - as we learned above, PHP can already vì chưng it, the right way.
P.S.Sometimes there is no error but no results either. Then it means, there is no data in the database lớn match your criteria. In this case you have to lớn admit this fact, even if you can swear the data & the criteria are all right. They are not. You have to kiểm tra them again. I"ve got an article that can help in this matter, How to debug database interactions. Although it is written for PDO, but the principle is the same. Just follow this instruction step by step & either have your problem solved or have an answerable question for Stack Overflow.