views:

404

answers:

5

Hi I am trying to select data from table but get this error message:

 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'];
}
+1  A: 

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 = $_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'];
}
scompt.com
Right, but using a die() if the query fails is a little to much.
Kau-Boy
I was going to design an entire error handling mechanism for the OP, but decided that might be beyond the scope of my answer.
scompt.com
+1  A: 

Put quotes around $username. String values, as opposed to numeric values, must be enclosed in quotes.

$result = mysql_query("SELECT * FROM Users WHERE UserName LIKE '$username'");

Also, there is no point in using the LIKE condition if you're not using wildcards: if you need an exact match use = instead of LIKE.

kemp
+2  A: 

There could be problem with single Quotes ('), you can put your query like this

mysql_query("SELECT * FROM Users WHERE UserName LIKE '".$username."'");
nik
Why adding noise with string concatenation instead of just putting the variable in the query string?
kemp
A: 

As scompt.com explained, the query might fail. Use this code the get the error of the query or the correct result:

$username = $_POST['username'];
$password = $_POST['password'];
if($result = mysql_query('SELECT * FROM Users WHERE UserName LIKE '.mysql_real_escape_string($username)))
{
    while($row = mysql_fetch_array($result))
    {
        echo $row['FirstName'];
    }
} else {
    echo 'Invalid query: ' . mysql_error() . "\n";
    echo 'Whole query: ' . $query; 
}

See http://www.php.net/manual/en/function.mysql-query.php for further information.

EDIT: And the actual error was the single quotes so that the var $username was not parsed. But you should really use mysql_real_escape_string($username) to avoid SQL injections.

Kau-Boy
A: 

Your code should be something like this

$username = $_POST['username']; $password = $_POST['password']; $query = "SELECT * FROM Users WHERE UserName LIKE '$username'"; echo $query; $result = mysql_query($query);

if($result === FALSE) { die(mysql_error("error message for the user")); }

while($row = mysql_fetch_array($result)) { echo $row['FirstName']; }

Once done with that, you would get the query printed on the screen. Try this query on your server and see if it produces the desired results. Most of the times the error is in the query. Rest of the code is correct.

Chaitannya