views:

97

answers:

1

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

$str = '[email protected]';

$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?

A: 
$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().

JP
Thanks. But whats the different between num_rows and fetch_row? Is fetch_row better? Why??
mysqllearner
Their tasks are different. When you execute a SELECT query, it returns a result. That result may contain row of data you selected. mysql_fetch_row() returns the next row available in the result set, while mysql_num_rows() returns the number of rows available in the result set.
JP