0

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.

1
  • Really weird. Change your query to select * from all_users and update us with the result , please. Commented Mar 7, 2012 at 11:47

3 Answers 3

1

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

Sign up to request clarification or add additional context in comments.

1 Comment

Makes sense, but I've done it in this order before and it worked, even when there's only 1 row found. Anyway, thanks, works now.
1

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);

Comments

0

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😜

1 Comment

Out of curiosity, how did you find this question 8 years after it was posted? :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.