Page 1 of 1

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

Posted: Sat Jul 05, 2008 1:01 pm
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?

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

Posted: Sat Jul 05, 2008 3:48 pm
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

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

Posted: Sat Jul 05, 2008 4:01 pm
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.

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

Posted: Sat Jul 05, 2008 5:00 pm
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

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

Posted: Sat Jul 05, 2008 5:11 pm
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.

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

Posted: Sun Jul 06, 2008 12:11 am
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.