Reputation: 3787
I have a simple mysql query checking the database table 'all_users' for an e-mail which was entered in a text field.
The e-mail is in a variable called $email
.
In my test case, $email
is [email protected] and that e-mail exists in the database table.
When I do this:
$result=mysql_query("select * from all_users where email='$email' ") or die (mysql_error());
$row=mysql_fetch_array($result);
$num=mysql_num_rows($result);
$num
is zero even though $row
is found.
So when I do echo $row['email'];
it correctly prints out '[email protected]' but when I do echo $num;
it's 0!
What am I doing wrong here? I've done this a thousand times but never had this issue.
Upvotes: 0
Views: 4279
Reputation: 1
I think it's not because of your mysqli_num_rows
function, do check the search query, whether it's working or not, otherwise use trim(column_name)
instead of simply giving column.
It worked for me... it tooks me 2 days to verify it😜
Upvotes: 0
Reputation: 88647
If you reverse the order of the statements, it will work as you expect. You are retrieving the row from the resource before asking it how many rows it has left. Since you are evidently only finding one row, this results in 0
, because you have already retrieved that row.
If you do this it should work:
$num = mysql_num_rows($result);
$row = mysql_fetch_array($result);
Upvotes: 1
Reputation: 13755
From http://php.net/manual/en/function.mysql-fetch-array.php - Returns an array that corresponds to the fetched row and moves the internal data pointer ahead
So change the order, i.e. first use mysql_num_rows
and only then do mysql_fetch_array
Upvotes: 1