Jump to content

[SOLVED] PHP newbie, can't understand error message! :(


Recommended Posts

Hey, I'm new here - I hope I'm posting this at the right place, and if I'm not I'm sorry. Please bare with me, I'm a PHP newbie. Anyway... I've checked over the code over and over and I can't seem to find any errors, so I hope you can help me. The error message i get is this:

 

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

 

the code is below:

 

<?

$host = "localhost";
$username = "XXXX";
$password = "XXXX";
$db_name = "XXXXXXXX_test1";
$tbl_name = "Login";

mysql_connect($host, $username, $password) or die("Failed to connect to mysql!");
mysql_select_db($db_name) or die ("Failed to connect to mysql!");

$myusername = $_POST['myusername'];
$mypassword = $_POST['mypassword'];

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

$count = mysql_num_rows($result);

if($count==1){
session_register("myusername");
session_register("mypassword");
header("location:login_success.php");
}
else {
echo "Wrong Username or Password";
}
?>

That particular error means that your query failed to even execute and a FALSE value was returned instead of a result resource (which is why the error mentions that the - supplied argument is not a valid MySQL result resource.) A query that execuits but matches zero rows is a successful query and returns a valid result resource and does not produce php errors when it is accessed.

 

Your query looks correct. That either means there is something in the data from the form that you put into the query that is breaking the query syntax (you need to use mysql_real_escape_string() on the string values put into the query to prevent sql injection and to prevent sql special characters that might be in the data from breaking the query syntax) or your table name/columns have some problem.

 

echo $sql; so that you can see exactly what is in it and echo mysql_error(); right after the mysql_query() statement to see what if any errors are being produced.

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.