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();
?>

 

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>";
.....

 

 

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

 

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.

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>

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>

<?
// 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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.