Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

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

Posts: 93
Trades: 0
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";} 
drew22299 is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 04-28-2008, 11:33 AM Re: How to use IF statement with mysql result
Extreme Talker

Posts: 177
Trades: 0
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 11:45 AM..
kbfirebreather is offline
Reply With Quote
View Public Profile
 
Old 04-28-2008, 11:39 AM Re: How to use IF statement with mysql result
Average Talker

Posts: 18
Name: TK
Trades: 0
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 11:41 AM..
zxcvbnm60 is offline
Reply With Quote
View Public Profile
 
Old 04-29-2008, 10:50 AM Re: How to use IF statement with mysql result
addonchat's Avatar
Super Talker

Posts: 115
Name: Chris Duerr
Trades: 0
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

Please login or register to view this content. Registration is FREE
-
Please login or register to view this content. Registration is FREE

Please login or register to view this content. Registration is FREE
addonchat is offline
Reply With Quote
View Public Profile
 
Old 04-29-2008, 01:36 PM Re: How to use IF statement with mysql result
drew22299's Avatar
Skilled Talker

Posts: 93
Trades: 0
Thanks, zxcvbnm60
drew22299 is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to How to use IF statement with mysql result
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

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

BB 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.15291 seconds with 12 queries