Author Topic: [SOLVED] COUNT not working  (Read 346 times)

0 Members and 1 Guest are viewing this topic.

Offline xstevey_bxTopic starter

  • Irregular
  • Posts: 34
    • View Profile
[SOLVED] COUNT not working
« on: February 24, 2009, 12:09:59 PM »

<?php
function newmessages() {
	
$query "SELECT COUNT(id) FROM messages WHERE to_id='$_SESSION[uid]'";
	
$result mysql_query($query);
	
$row mysql_fetch_assoc($result);
	
$num_rows $row['COUNT(id)'];

	
echo 
'Message Inbox (' $num_rows .')';
}

?>


The above statement works, However when I introduce an AND statement it breaks, even though the column name is correct... I dont know whats going on?


<?php
function newmessages() {
	
$query "SELECT COUNT(id) FROM messages WHERE to_id='$_SESSION[uid]' AND read='0'";
	
$result mysql_query($query);
	
$row mysql_fetch_assoc($result);
	
$num_rows $row['COUNT(id)'];

	
echo 
'Message Inbox (' $num_rows .')';
}

?>
« Last Edit: February 24, 2009, 12:10:34 PM by xstevey_bx »

Offline neil.johnson

  • Guru
  • Fanatic
  • *
  • Posts: 3,416
  • Gender: Male
    • View Profile
Re: COUNT not working
« Reply #1 on: February 24, 2009, 12:17:49 PM »
Can you print the mysql error using
mysql_query($query) or die(mysql_error());
Quote
To start, press any key. Where's the 'Any' key?

Offline xstevey_bxTopic starter

  • Irregular
  • Posts: 34
    • View Profile
Re: COUNT not working
« Reply #2 on: February 24, 2009, 12:21:03 PM »
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'read='0'' at line 1

Lol I could have told you that haha :P

I know it had something to do with the AND part of the statement

Offline neil.johnson

  • Guru
  • Fanatic
  • *
  • Posts: 3,416
  • Gender: Male
    • View Profile
Re: COUNT not working
« Reply #3 on: February 24, 2009, 12:24:35 PM »
READ is a mysql reserved word. You cannot use it as a field name.

http://dev.mysql.com/doc/refman/5.1/en/reserved-words.html
« Last Edit: February 24, 2009, 12:25:13 PM by neil.johnson »
Quote
To start, press any key. Where's the 'Any' key?

Offline xstevey_bxTopic starter

  • Irregular
  • Posts: 34
    • View Profile
Re: COUNT not working
« Reply #4 on: February 24, 2009, 12:26:34 PM »
Bingo!! thanks so much!