Current location: Hot Scripts Forums » Programming Languages » PHP » Problem in function?

Problem in function?

Reply
  #1 (permalink)  
Old 02-08-04, 10:40 AM
musicalmidget's Avatar
musicalmidget musicalmidget is offline
Newbie Coder
 
Join Date: Jun 2003
Location: United Kingdom
Posts: 21
Thanks: 0
Thanked 0 Times in 0 Posts
Problem in function?

Hi.

I'm having a few problems with some code on my site.

Untill now, my "messages" function was...

PHP Code:
function messages() {
$sql = "SELECT * FROM messages";
$query = mysql_query($sql);
$count = mysql_num_rows($query);
?>
<tr>
<td class="table">There are currently <a href="messages.php"><b><?php echo $count ?></b></a> active messages.</td>
</tr>
<?php
}
...and everything worked fine.

However, I recently tried adding a new feature to the function and changed the code to this...

PHP Code:
function messages() {
$sql = "SELECT * FROM messages WHERE read='no'";
$query = mysql_query($sql);
$count = mysql_num_rows($query);
?>
<tr>
<td class="table">There are currently <a href="messages.php"><b><?php echo $count ?></b></a> unread messages.</td>
</tr>
<?php
}
...and the code no longer works. I am presented with this error message instead:

Quote:
Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource.
I added the new field to the database and everything so it is set up correctly as far as I can tell, but I just can't understand why I'm getting this error.

I've been trying for a while to fix it and still just can't be sure what is causing the problem, so if someone could enlighten me I would be very grateful.

Thanks.
__________________
Has anyone seen my signature?
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Share on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #2 (permalink)  
Old 02-08-04, 12:07 PM
mdhall's Avatar
mdhall mdhall is offline
Aspiring Coder
 
Join Date: Oct 2003
Posts: 502
Thanks: 0
Thanked 0 Times in 0 Posts
Make sure that the values stored in the database match the query criteria, i.e., things like "no" and "No" must be the same. If you have WHERE read='no', then the data in the db must also be saved as 'no', not as 'No' which is capitalized. Sounds like something very basic, I know, but someting to double-check.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Share on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #3 (permalink)  
Old 02-08-04, 12:11 PM
musicalmidget's Avatar
musicalmidget musicalmidget is offline
Newbie Coder
 
Join Date: Jun 2003
Location: United Kingdom
Posts: 21
Thanks: 0
Thanked 0 Times in 0 Posts
It usually is something basic with me.

Unfortunately though, that's not it. "No" is all in lowercase in the database, just as it is written in the query.
__________________
Has anyone seen my signature?
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Share on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #4 (permalink)  
Old 02-08-04, 12:26 PM
mdhall's Avatar
mdhall mdhall is offline
Aspiring Coder
 
Join Date: Oct 2003
Posts: 502
Thanks: 0
Thanked 0 Times in 0 Posts
The table name you are querying ( "read" ) is the same as the db field name?

Maybe try something like this and see what you get...

function messages() {
$query = mysql_query("SELECT * FROM messages WHERE read='no'") or die (mysql_error());
$result= mysql_query($query);
$count = mysql_num_rows($result
);
if(mysql_num_rows($result) == 0){
echo("Nothing to Display!");
}
else
{
echo "$count results";
}
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Share on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #5 (permalink)  
Old 02-08-04, 01:10 PM
musicalmidget's Avatar
musicalmidget musicalmidget is offline
Newbie Coder
 
Join Date: Jun 2003
Location: United Kingdom
Posts: 21
Thanks: 0
Thanked 0 Times in 0 Posts
Hi.

When I tried the code above I got this error instead:

Quote:
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='no'' at line 1
The name of the field in the database is definately "read", but there is definately something it doesn't like about it. :-/
__________________
Has anyone seen my signature?
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Share on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #6 (permalink)  
Old 02-08-04, 11:27 PM
nd2 nd2 is offline
Wannabe Coder
 
Join Date: Jun 2003
Posts: 128
Thanks: 0
Thanked 0 Times in 0 Posts
you will find that read is actualy a mysql reserved word.
there is however a way arround it by simply placing backticks arround the table name which are normal not required.

SELECT * FROM messages WHERE `read`='no'
__________________
IonCMS (Coming Soon.)
http://ioncms.com
--
Ncaster (Free php/mysql cms)
http://ncaster.cjb.net
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Share on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #7 (permalink)  
Old 02-09-04, 04:44 AM
mdhall's Avatar
mdhall mdhall is offline
Aspiring Coder
 
Join Date: Oct 2003
Posts: 502
Thanks: 0
Thanked 0 Times in 0 Posts
I didn't know that. Thanx.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Share on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #8 (permalink)  
Old 02-09-04, 10:05 AM
musicalmidget's Avatar
musicalmidget musicalmidget is offline
Newbie Coder
 
Join Date: Jun 2003
Location: United Kingdom
Posts: 21
Thanks: 0
Thanked 0 Times in 0 Posts
Brilliant, everything works fine now.

I've seen queries like this before but didn't know why they were written in this way. I'll bare this in mind for the future.

Thanks for the help.
__________________
Has anyone seen my signature?
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Share on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #9 (permalink)  
Old 02-09-04, 11:43 PM
nd2 nd2 is offline
Wannabe Coder
 
Join Date: Jun 2003
Posts: 128
Thanks: 0
Thanked 0 Times in 0 Posts
no problem.
__________________
IonCMS (Coming Soon.)
http://ioncms.com
--
Ncaster (Free php/mysql cms)
http://ncaster.cjb.net
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Share on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

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 Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Classified Ads skipper23 Perl 3 11-22-05 02:22 AM
accessing existing ISP email with a PHP webmail script. nlancaster PHP 1 01-07-04 03:28 AM
Classified Ads skipper23 Perl 2 12-30-03 03:43 AM
Help trim code down TheLaughingBandit JavaScript 0 09-02-03 09:50 AM
File Management, sorry i keep changing, this is the last one! :) Arctic ASP 28 08-29-03 12:04 AM


All times are GMT -5. The time now is 09:09 AM.
vBulletin® Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.