开发者

Mysql is not counting the rows of dates from my php script

开发者 https://www.devze.com 2023-02-28 07:52 出处:网络
I am trying to count the number of dates that occur for a specific mem_id, however, my count output is always \"0\"... heres my code:

I am trying to count the number of dates that occur for a specific mem_id, however, my count output is always "0"... heres my code:

$datechecker = $postdate; 
//$postdate is a date variable that is posted via a form into mysql, stored as a DATE

$sql = my开发者_如何转开发sql_query("SELECT * FROM members WHERE mem_id='$id' AND postdate='$datechecker'");
$Counter = mysql_num_rows($sql); 
if($Counter >= 0) {
echo "$datechecker $Counter";
exit();
}

This is the output I get: 2011-01-01 0

Even though I have about 10 occurrences of 2011-01-01, so why does my count say "0"? Can anyone help me solve this, or provide an alternate?


echo the string before you mysql_query it, this will validate if the query is what you expect it to be. You can also use count(*) within the query, instead of using php's mysql_num_rows()

I also hope that you're sanitising your input before you're querying that!


1) You didn't clean $postdate, who knows if it conforms to MySQL's definition of DATE?

2) You didn't show us how your members table looks like. Is mem_id primary key? If it is, then of course you're going to get 1 row out of it.

3) You aren't checking whether your query succeeds or not, you immediately pass the resource_id to the mysql_num_rows table - which is a wrong way to perform counting anyway because MySQL (like any other relational database) has inbuilt mechanisms of counting rows based on criteria.

My guess is that your query is failing, seeing there's not $id specified.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号