Differences case sensitivity in SELECT COLUMN and COUNT, mysql?
-
11-09-2019 - |
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?
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().