if(mysql_num_rows($result)) inside if (mysql_num_ro[SOLVED]

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
james3302
Forum Commoner
Posts: 53
Joined: Thu Aug 02, 2007 11:11 am

if(mysql_num_rows($result)) inside if (mysql_num_ro[SOLVED]

Post by james3302 »

Here's my code.

Code: Select all

 
$query  = "SELECT username, password,lastLogin FROM users WHERE username='$user'";
$result = mysql_query($query);
 
//verify login information
if (mysql_num_rows($result)){
    $row = mysql_fetch_array($result, MYSQL_ASSOC);
    if($password == $row['password']){
        $query = "UPDATE users SET lastLogin = '$LastLogin' WHERE username = '$user'";
        mysql_query($query) or die('Error, query failed');
        echo "<center><h2>Welcome $user !</h2></center>";
        echo "Last Login was on " . $row['lastLogin'];
        echo "<br><br><br><br><a href=search.php?user=$user>Search Users</a>";
 
        //check to see if user has any friends
        $query  = "SELECT username, friendname FROM Friends WHERE username='$user'";
        $result = mysql_query($query);
        echo "<br><u><h2>Friends</h2></u>";
        while($row = mysql_fetch_array($result, MYSQL_ASSOC)){
                $friendname = $row['friendname'];
                echo $row['friendname'] . "   <a href=createmsg.php?friendname=$friendname&user=$user>Send Msg</a><br>";
        }
        
       //check for any unread messages
        $query  = "SELECT user FROM msg WHERE friendname = '$user' and read = '0'";
        $result = mysql_query($query);
        if (mysql_num_rows($result)){ //here's line 33 where the error occurs. 
           echo "<br><u><h2><a href = readmsgs.php?user = $user>You have new Mgs</a></h2></u>";
                
        }
 
I know there are not enough end brackets, I have some else statements that work so I did not paste them here.

Here's my error
Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/whatith/public_html/Examples/login.php on line 33

any ideas? I probably should use functions to make this more easier to follow!

Oh yea I have Main.php where the user enters their username/password, when they hit the login button it takes them to the login.php file(which is this file) that verifies the username/password. After I verify the username/password should I call another page to create the users main page?
Last edited by james3302 on Mon Jul 07, 2008 7:06 am, edited 1 time in total.
User avatar
jaoudestudios
DevNet Resident
Posts: 1483
Joined: Wed Jun 18, 2008 8:32 am
Location: Surrey

Re: if (mysql_num_rows($result)) inside if (mysql_num_rows($resu

Post by jaoudestudios »

It is showing a warning because the sql query returned no results.

when using mysql_num_rows use an if statement

i.e.

Code: Select all

$sql = mysql_query("SQL QUERY");
if ($sql) {
 $size = mysql_num_rows($sql);
}
Hope that helps
james3302
Forum Commoner
Posts: 53
Joined: Thu Aug 02, 2007 11:11 am

Re: if (mysql_num_rows($result)) inside if (mysql_num_rows($resu

Post by james3302 »

Code: Select all

 
# //check for any unread messages
#         $query  = "SELECT user FROM msg WHERE friendname = '$user' and read = '0'";
#         $result = mysql_query($query);
#         if (mysql_num_rows($result)){ //here's line 33 where the error occurs.
#            echo "<br><u><h2><a href = readmsgs.php?user = $user>You have new Mgs</a></h2></u>";
#                
#         }
 
I did use an if statment. I did not on the one before it thought, but the error comes up on this section of code.
User avatar
jaoudestudios
DevNet Resident
Posts: 1483
Joined: Wed Jun 18, 2008 8:32 am
Location: Surrey

Re: if (mysql_num_rows($result)) inside if (mysql_num_rows($resu

Post by jaoudestudios »

Try this...

Code: Select all

$result = mysql_query($query);
 if ($result){ 
  if (mysql_num_rows($result) > 0) {
   echo "<br><u><h2><a href = readmsgs.php?user = $user>You have new Mgs</a></h2></u>";               
 }
}
You could use 1 if I guess, if you want to be tidy
User avatar
jayshields
DevNet Resident
Posts: 1912
Joined: Mon Aug 22, 2005 12:11 pm
Location: Leeds/Manchester, England

Re: if (mysql_num_rows($result)) inside if (mysql_num_rows($resu

Post by jayshields »

Try changing your mysql_query($query) statement for:

Code: Select all

mysql_query($query) or die(mysql_error() . '<br />' . $query)
to see if it gives you a helpful error message.

As far as I can tell, the only useful thing jaoudestudios said was when he recommended that you put > 0 after your mysql_num_rows() call in your if statement.
james3302
Forum Commoner
Posts: 53
Joined: Thu Aug 02, 2007 11:11 am

Re: if (mysql_num_rows($result)) inside if (mysql_num_rows($resu

Post by james3302 »

I figured it out, this is the 2nd time I have used a reserved word(i assume) for a column name. I had read as a column name in MySQL. Read is used as a Y OR N to keep track of the read and unread messages. I changed it to opened and my code worked fine.
Post Reply