I would like to get the number of rows that satisfies the condition.
mysql_query("SELECT COUNT(*) FROM sid WHERE sid='".session_id()."'");
this one ignores the condition.
update:
$session = session_id();
$sql = "SELECT COUNT(*) as row_count FROM sid WHERE sid = '$session' ";
var_dump($r = mysql_query($sql));//resource(4) of type (mysql result) (1)
var_dump(mysql_fetch_assoc($r));
//array(1) { ["row_count"]=> string(1) "1" } - this result is OK(2)
(1) resource(4) - I thought that 4 was the count
(2) mysql_real_escape_string($_SESSION['id']); gives 0
note:
I have changed from mysql_num_rows to this type of getting count because I thought it will return immediately the count and I dont have to write more lines to get this basic data.
session_id()can be manipulated by the user in anyway (i.e. comes from a cookie or URL parameter - not a PHP specialist), this code is vulnerable to SQL injection and should be fixed.';are not valid characters in a session id, and if you send an invalid session id, a call tosession_start()will create a new session and generate a new id. Still +1 for your comment, though, as I'm not 110% sure about this.;is not a possible vector for SQL-injection attacks.mysql_querydoes not allow multiple queries, the XKCD is incorrect and that attack is impossible. What is possible is a' or (1=1) UNION SELECT username, password, email FROM users --kind of injection. Of course'can be encoded in clever ways. You should never rely on internals, but just escape everything before injecting it into the SQL-statement.