Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mysql_fetch_array()/mysql_fetch_assoc()/mysql_fetch_row()/mysql_num_rows etc... expects parameter 1 to be resource

Tags:

php

mysql

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

mysql_fetch_array() expects parameter 1 to be resource, boolean given

This is my code:

$username = $_POST['username']; $password = $_POST['password'];  $result = mysql_query('SELECT * FROM Users WHERE UserName LIKE $username');  while($row = mysql_fetch_array($result)) {     echo $row['FirstName']; } 
like image 773
iamjonesy Avatar asked Jun 04 '10 10:06

iamjonesy


People also ask

What is mysql_fetch_array () function?

mysql_fetch_array is a PHP function that will allow you to access data stored in the result returned from the TRUE mysql_query if u want to know what is returned when you used the mysql_query function to query a Mysql database. It is not something that you can directly manipulate. Syntax.

What is difference between mysql_fetch_array and Mysql_fetch_row?

mysqli_fetch_array() is an extended version of the mysqli_fetch_row() function. In addition to storing the data in the numeric indices of the result array, the mysqli_fetch_array() function can also store the data in associative indices, using the field names of the result set as keys.

What is the difference between mysql_fetch_array () and mysql_fetch_assoc ()?

Difference: mysql_fetch_assoc() will always assing a non-secuencial key (like "color" and not a number). mysql_fetch_array() will assing a number key if there are not "word" key (0 and not "color" if there are not "color") but, you can choose to assing of key with a parameter...

What is mysql_fetch_assoc function?

The fetch_assoc() / mysqli_fetch_assoc() function fetches a result row as an associative array. Note: Fieldnames returned from this function are case-sensitive.


1 Answers

A query may fail for various reasons in which case both the mysql_* and the mysqli extension will return false from their respective query functions/methods. You need to test for that error condition and handle it accordingly.

mysql_* extension:

NOTE The mysql_ functions are deprecated and have been removed in php version 7.

Check $result before passing it to mysql_fetch_array. You'll find that it's false because the query failed. See the mysql_query documentation for possible return values and suggestions for how to deal with them.

$username = mysql_real_escape_string($_POST['username']); $password = $_POST['password']; $result = mysql_query("SELECT * FROM Users WHERE UserName LIKE '$username'");  if($result === FALSE) {      die(mysql_error()); // TODO: better error handling }  while($row = mysql_fetch_array($result)) {     echo $row['FirstName']; } 

mysqli extension
procedural style:

$username = mysqli_real_escape_string($mysqli, $_POST['username']); $result = mysqli_query($mysqli, "SELECT * FROM Users WHERE UserName LIKE '$username'");  // mysqli_query returns false if something went wrong with the query if($result === FALSE) {      yourErrorHandler(mysqli_error($mysqli)); } else {     // as of php 5.4 mysqli_result implements Traversable, so you can use it with foreach     foreach( $result as $row ) {         ... 

oo-style:

$username = $mysqli->escape_string($_POST['username']); $result = $mysqli->query("SELECT * FROM Users WHERE UserName LIKE '$username'");  if($result === FALSE) {      yourErrorHandler($mysqli->error); // or $mysqli->error_list } else {     // as of php 5.4 mysqli_result implements Traversable, so you can use it with foreach     foreach( $result as $row ) {       ... 

using a prepared statement:

$stmt = $mysqli->prepare('SELECT * FROM Users WHERE UserName LIKE ?'); if ( !$stmt ) {     yourErrorHandler($mysqli->error); // or $mysqli->error_list } else if ( !$stmt->bind_param('s', $_POST['username']) ) {     yourErrorHandler($stmt->error); // or $stmt->error_list } else if ( !$stmt->execute() ) {     yourErrorHandler($stmt->error); // or $stmt->error_list } else {     $result = $stmt->get_result();     // as of php 5.4 mysqli_result implements Traversable, so you can use it with foreach     foreach( $result as $row ) {       ... 

These examples only illustrate what should be done (error handling), not how to do it. Production code shouldn't use or die when outputting HTML, else it will (at the very least) generate invalid HTML. Also, database error messages shouldn't be displayed to non-admin users, as it discloses too much information.

like image 194
Edward Dale Avatar answered Sep 21 '22 10:09

Edward Dale