Reply
How to use IF statement with mysql result
Old 04-28-2008, 04:29 AM How to use IF statement with mysql result
drew22299's Avatar
Skilled Talker

Posts: 89
Location: Wiltshire, UK
Hi,

I want to include a check that won't allow the message to be sent if the user's inbox has more than 25 messages.

I have tried the following query but it didn't work, can anyone see why?

PHP Code:
$messagesNum=(mysql_num_rows(mysql_query("SELECT userto, COUNT(userto FROM messages WHERE userto = '$userto'")));
if(
$messagesNum 25){
$msg=$msg."<img src='error.png'> Can't send message<BR>";
$status"NOTOK";} 
__________________
www.hotlista.co.uk
drew22299 is offline
Reply With Quote
View Public Profile Visit drew22299's homepage!
 
When You Register, These Ads Go Away!
Old 04-28-2008, 10:33 AM Re: How to use IF statement with mysql result
Super Talker

Posts: 122
PHP Code:
$messagesNum=mysql_num_rows(mysql_query("SELECT * FROM messages WHERE userto = '$userto'")); 
that should just do the number of rows, and will be however many rows they have in the table. I dont think that COUNT() function was necessary.

Last edited by kbfirebreather : 04-28-2008 at 10:45 AM.
kbfirebreather is offline
Reply With Quote
View Public Profile
 
Old 04-28-2008, 10:39 AM Re: How to use IF statement with mysql result
Average Talker

Posts: 18
Name: TK
A couple things

Your SQL query's parentheses are are off. As it is, you've coupled your FROM and WHERE clauses into the COUNT function. The result is probably a
userto: NULL count: 0.

I think you meant
Code:
SELECT userto, COUNT(*) FROM messages WHERE userto = '$userto'"
which again will only return a single row:
userto: John count: 43

You don't need to use both COUNT and mysql_num_rows here, since the COUNT is aggregating function. If userto is unique, only 1 row will be returned.

Using COUNT only
Code:
$result = mysql_query("SELECT userto, COUNT(*) as count FROM messages WHERE userto = '$userto'");
$row = mysql_fetch_assoc($result));
if ($row['count']>25) {
 $msg=$msg."<img src='error.png'> Can't send message<BR>";
$status= "NOTOK";
}


Using mysql_num_rows only
Code:
$messagesNum= mysql_num_rows(mysql_query("SELECT userto FROM messages WHERE userto = '$userto'"));
if ($messagesNum>25) {
 $msg=$msg."<img src='error.png'> Can't send message<BR>";
$status= "NOTOK";
}

Last edited by zxcvbnm60 : 04-28-2008 at 10:41 AM.
zxcvbnm60 is offline
Reply With Quote
View Public Profile
 
Old 04-29-2008, 09:50 AM Re: How to use IF statement with mysql result
addonchat's Avatar
Skilled Talker

Posts: 97
Name: Chris Duerr
Hmm - no need to "SELECT userto" when we already have $userto, right? Or, are we just sanitizing the data to update $userto to match the case as it is in the db?
__________________
Chris Duerr
AddonChat Java Chat Software
http://www.addonchat.com/ - Affiliate Program
addonchat is offline
Reply With Quote
View Public Profile
 
Old 04-29-2008, 12:36 PM Re: How to use IF statement with mysql result
drew22299's Avatar
Skilled Talker

Posts: 89
Location: Wiltshire, UK
Thanks, zxcvbnm60
__________________
www.hotlista.co.uk
drew22299 is offline
Reply With Quote
View Public Profile Visit drew22299's homepage!
 
Reply     « Reply to How to use IF statement with mysql result
 

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off




   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML

 


Page generated in 0.13138 seconds with 12 queries