You're assigning the number of rows to $query
, whereas you should be assigning the return value of mysql_query()
because mysql_fetch_array()
requires a result identifier as its argument.
Other issues:
- Usage of the deprecated MySQL library. Consider upgrading to PDO or MySQLi
- The source of
$email
is not shown but there may be an SQL Injection vulnerability. Use a prepared statement in PDO or MySQLi to prevent this.
- Check the return value before you try to fetch rows. If your query failed, you would be passing a boolean to
mysql_fetch_array()
.
Refactored to show the proper logic (but still should not be used because it's deprecated):
$query= mysql_query("SELECT * FROM members WHERE email='" . mysql_real_escape_string($email) . "'");
if($query){ // check the return value
while ($row = mysql_fetch_array($query)) {
$firstname = $row['firstname'];
}
}
MySQLi example using a prepared statement:
$db = new mysqli('localhost', 'user', 'pass', 'dbname');
if($stmt = $db->prepare('SELECT * FROM members WHERE email = ?')){
$stmt->bind_param('s', $email);
$stmt->execute();
if($result = $stmt->get_result()){
while ($row = $result->fetch_assoc()){
$firstname = $row['firstname'];
}
}
}