Jump to content

[SOLVED] Warning: mysql_num_rows(): supplied argument is not a valid MySQL result


sanderphp

Recommended Posts

I'm getting the following error on a login script from http://www.phpeasystep.com/workshopview.php?id=6

 

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/sanderan/public_html/php/cycling/checklogin.php on line 27

Wrong Username or Password

 

Is this really a password issue or something else? Here is my code.

 

<?php
ob_start();
$host="localhost"; // Host name 
$username="sanderan_stats"; // Mysql username 
$password="password"; // Mysql password 
$db_name="sanderan_cycling"; // Database name 
$tbl_name="riders"; // Table name 

// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
mysql_select_db("$db_name")or die("cannot select DB");

// Define $myusername and $mypassword 
$myusername=$_POST['myusername']; 
$mypassword=$_POST['mypassword']; 

// To protect MySQL injection (more detail about MySQL injection)
$myusername = stripslashes($myusername);
$mypassword = stripslashes($mypassword);
$myusername = mysql_real_escape_string($myusername);
$mypassword = mysql_real_escape_string($mypassword);

$sql="SELECT * FROM $tbl_name WHERE username='$myusername' and password='$mypassword'";
$result=mysql_query($sql);

// Mysql_num_row is counting table row
$count=mysql_num_rows($result);
// If result matched $myusername and $mypassword, table row must be 1 row

if($count==1){
// Register $myusername, $mypassword and redirect to file "login_success.php"
session_register("myusername");
session_register("mypassword"); 
header("location:login_success.php");
}
else {
echo "Wrong Username or Password";
}

ob_end_flush();
?>

Link to comment
Share on other sites

 

Maybe you can print for us your query string as well as any errors from your query.

 

.....
$sql="SELECT * FROM $tbl_name WHERE username='$myusername' and password='$mypassword'";
print "$sql<br>";
$result=mysql_query($sql);
print mysql_error()."<br>";
.....

 

 

Link to comment
Share on other sites

Unless I misunderstood what you were asking for, I added your code into mine.

 

Here is the result:

 

SELECT * FROM riders WHERE username='' and password=''

Unknown column 'username' in 'where clause'

 

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/sanderan/public_html/php/cycling/checklogin.php on line 29

Wrong Username or Password

Link to comment
Share on other sites

 

It appears that your table does not have a field called "username". As a result mysql_query($sql) fails and $result becomes invalid handle and gives the warning error when you use it afterwards.

 

Check your table definition for the "username" field and probably it is misspelled.

Link to comment
Share on other sites

aha.... I was using 'login' and the example was using username.

 

It now goes to the login_success.php page and shows me this. Anything to worry about or is this what I should be seeing?

 

// Check if session is not registered , redirect back to main page. // Put this code in first line of web page.

Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at /home/sanderan/public_html/php/cycling/login_success.php:3) in /home/sanderan/public_html/php/cycling/login_success.php on line 4

Login Successful

 

// Check if session is not registered , redirect back to main page. 
// Put this code in first line of web page. 
<? 
session_start();
if(!session_is_registered(myusername)){
header("location:main_login.php");
}
?>

<html>
<body>
Login Successful
</body>
</html>

Link to comment
Share on other sites

Here is all that I have in the login_success.php file

 

// Check if session is not registered , redirect back to main page. 
// Put this code in first line of web page. 
<? 
session_start();
if(!session_is_registered(myusername)){
header("location:main_login.php");
}
?>

<html>
<body>
Login Successful
</body>
</html>

Link to comment
Share on other sites

<?
// Check if session is not registered , redirect back to main page. 
// Put this code in first line of web page.  
session_start();
if(!session_is_registered(myusername)){
header("location:main_login.php");
}
?>

<html>
<body>
Login Successful
</body>
</html>

 

The above works.

 

You needed to move the comment inside the php tags to stop it displaying - which is the output that caused the error.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.