Thursday 15 July 2010

php - mysqli_fetch_array()/mysqli_fetch_assoc()/mysqli_fetch_row() expects parameter 1 to be resource or mysqli_result, boolean given -


I am trying to select data from a MySQL table, but I get one of the following error messages:

mysql_fetch_array () parameter 1 is expected to be processed, the boolean given

or

mysqli_fetch_array () The value of 1 paragraph mysqli_result, boolean given

This is my code:

  $ username = $ _POST ['username']; $ Password = $ _POST ['password']; $ Result = mysql_query ('Username Username Where $ Username'); While ($ line = mysql_fetch_array ($ result)) {echo $ line ['first name']; }  

applies to the same code as

  $ result = mysqli_query ($ mysqli, 'SLECT ...'); // mysqli_fetch_array () parameter 1 is expected to be mysqli_result, while the bullion is given ($ line = mysqli_fetch_array ($ result)) {...  

and

< Pre> $ result = $ mysqli-> Query ($ mysqli, 'SELCT ...'); // Call a fetch_assoc () member function on a non-object ($ line = $ result-> fetch_assoc ($ result)) {...

and

  $ result = $ pdo-> Query ('SLECT ...', PDO :: FETCH_ASSOC); // foreach () foreign currency ($ result as $ line) {...  

and

  $ stmt = $ mysqli-> Invalid argument was provided for; Prepare ('Choose ...'); // a non-object $ stmt-> gt; Call bind_param () to bind_param (...)  

and

  $ stmt = $ Pdo-> Prepare ('SLECT ...'); // Call a member-item bindParam () on non-object $ stmt-> A bind pattern (...)  

A query may fail for many reasons, whose In case both mysql_ * and mysqli extensions will return false to their respective functions / methods. You must test for the condition of that error and handle it accordingly.

:

Note and deleted in Php version 7. Check $ result before going to mysql_fetch_array

. You will find that this is wrong because the query is unsuccessful. See the documentation for suggesting possible return values ​​and how to deal with them.

  $ username = mysql_real_escape_string ($ _ POST ['username']); $ Password = $ _POST ['password']; $ Correction = mysql_query ("user name" 'user name' ');; if ($ result === incorrect) {dead (mysql_error ()); // TODO: better error handling} while ($ row = Mysql_fetch_array ($ result)) {echo $ line ['FirstName'];}  


procedural style :

$ username = mysqli_real_escape_string ($ mysqli, $ _POST ['user name']); $ Result = mysqli_query ($ mysqli, "Select * from user where 'username' '$ username'" ); // mysqli_query if wrong goes wrong in query if ($ result === FALSE) {yourErrorHandler (mysqli_e Rror ($ mysqli));} Else {// php 5.4 mysqli_result as transable, you can use it with foreach foreach ($ $ as the $ result) {...

oo-style :

  $ username = $ mysqli- & gt; escape_string ($ _ POST ['username']); $ result = $ Mysqli- & gt; query (select 'user username' on user name from "user"); If ($ result === FALSE) {yourErrorHandler ($ mysqli-> error;); // or $ mysqli- & gt; Error_list} executes 5.4 mysqli_result as the other {// php}, so that you can use it with foreach foreach ($ $ as the $ line of results) {...  
< P> using a prepared statement:

  $ stmt = $ mysqli-> Prepare ('Select from users where the user name is?'); If (! $ Stmt) {yourErrorHandler ($ mysqli-> Error;); // or $ mysqli- & gt; Error_list} and if (! $ Stmt- & gt; Dam_rem ('s', $ _POST ['username'])) {yourErrorHandler ($ stmt-> error); // or $ stmt-> Error_list} and if (! $ Stmt- & gt; Execute ()) {yourErrorHandler ($ stmt-> Error;); // or $ stmt-> Error_list} and {$ result = $ stmt-> Get_result (); // php 5.4 mysqli_result as a tool traversable, you can use it with foreach foreach ($ $ as the $ result) {...  

Example should be what should be for example (error handling), how to do it The output code should not be used when output to HDM, otherwise it will generate (at least) invalid HTML. Additionally, the database error message should not be displayed to non-administrator users, as well.


No comments:

Post a Comment