/    Sign up×
Community /Pin to ProfileBookmark

COUNT not working?

COUNT not working.

Getting this error:

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

My Code:

<?php

$num = mysql_query(“SELECT COUNT(id) FROM emails WHERE user_id = ‘$username’ AND read = ‘0’”)or die(mysql_error());
while($count = mysql_fetch_array($num))
{
echo “(<strong> “.$count[‘COUNT(id)’].” </strong>)”;
}
?>

Any Suggesstions?

to post a comment
PHP

12 Comments(s)

Copy linkTweet thisAlerts:
@ellisglAug 10.2007 — SELECT COUNT(id) FROM emails WHERE user_id = '$username' AND read = '0'"
Copy linkTweet thisAlerts:
@bjs1280authorAug 10.2007 — Doesn't work, I get this error Undefined index: COUNT(id)
Copy linkTweet thisAlerts:
@ellisglAug 10.2007 — Try this: SELECT COUNT(id) FROM emails WHERE user_id = '$username' AND read = '0'"
Copy linkTweet thisAlerts:
@bjs1280authorAug 10.2007 — That worked great thanks.
Copy linkTweet thisAlerts:
@bubbisthedogAug 10.2007 — bjs: Don't know if you're aware, but you are not required to put the single quotes around your table and field names. The following should be syntactically correct as well:
<i>
</i>SELECT COUNT(id)
FROM emails
WHERE user_id = $username
AND read = '0'

Might make things a little simpler. ?
Copy linkTweet thisAlerts:
@ellisglAug 10.2007 — I think that READ is a reserved word in MySQL. That is why you need to put 's around your column and table names. I went over board on the first one. Rule of thumb - always use 's on column and table names when making your query string.
Copy linkTweet thisAlerts:
@ellisglAug 10.2007 — bjs: Don't know if you're aware, but you are not required to put the single quotes around your table and field names. The following should be syntactically correct as well:
<i>
</i>SELECT COUNT(id)
FROM emails
WHERE user_id = $username
AND read = '0'

Might make things a little simpler. ?[/QUOTE]


Yeah - but if you have a table or column name that is a reserved word or a number - then you end up with errors. Also I think values can also screw up a query too if they are not quoted (ie. column = $value ($value is a string with that just has the word AND)) - Which I need to test that theory...
Copy linkTweet thisAlerts:
@bubbisthedogAug 10.2007 — Yeah - but if you have a table or column name that is a reserved word or a number - then you end up with errors. Also I think values can also screw up a query too if they are not quoted (ie. column = $value ($value is a string with that just has the word AND)) - Which I need to test that theory...[/QUOTE]
Perfectly valid point, although I can not remember a time in all the years I've worked with SQL when I accidentally named a field or table a reserved word. I always omit them personally, but that's good advice. ?

EDIT: Sorry, didn't see that they'd originally used no single quotes. :o My bad, I'll bud out. ?
Copy linkTweet thisAlerts:
@bjs1280authorAug 10.2007 — you were right, once I changed the read to read_pm everything worked fine. Thanks for the advice guys.
Copy linkTweet thisAlerts:
@ellisglAug 10.2007 — Nah it's cool. I used to do the no quotes thing untill I started having tables name that had a naming convention of YearMonth then someone told me the same thing here - also use `'s and single quotes. Something about strict rules of SQL or something that should be followed...
Copy linkTweet thisAlerts:
@bjs1280authorAug 10.2007 — I'll keep that in mind.
Copy linkTweet thisAlerts:
@bubbisthedogAug 10.2007 — Nah it's cool. I used to do the no quotes thing untill I started having tables name that had a naming convention of YearMonth then someone told me the same thing here - also use `'s and single quotes. Something about strict rules of SQL or something that should be followed...[/QUOTE]
For all of my fields, I use a naming convention of [[I]table name abbreviation[/I]]_[[I]field name[/I]]. I make sure to keep track of my table name abbreviations so that none repeat. As a result, no single quotes are necessary.
×

Success!

Help @bjs1280 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...