Question

Okay, I am abit confusing here. In my database I have 5 rows of this data => "string@domail.com" (all lower case), and This is my code, Query 1 (I am using php and mysql):

$str = 'STRING@DOMAIN.COM';

$sel = mysql_query("SELECT COUNT(*) 
                    FROM table 
                    WHERE `column` = '{$str}'");
$num = mysql_num_rows($sel);

echo $num;

The result is 1. But if I change to Query2

SELECT column 
FROM table 
WHERE `column` = '{$str}'"

It returns 5.

And another question is, which query should I use if i want to find out the number of rows exist in database, query 1 or 2, in term of query speed?

Was it helpful?

Solution

$num = mysql_num_rows($sel);

This returns the number of rows selected from your query. For query one, since you select COUNT(*) from your table, it returns one single row with one cell into it: value of this cell is 5. The second query select all entries that have the value from $str, so mysql_num_rows($sel); will indeed return 5.

As for the other question, first query if more efficient, but if you're looking for the number of rows with value $str, do not use mysql_num_rows() but mysql_fetch_row().

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top