login script error

Hello i just installed a simple login script but im getting a error

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/myjesus2/public_html/loginscript.php on line 22
Invalid login information. Please return to the previous page.

html form code:

Username

Password

php script

<?php // All code was wrote by Tim Kipp @ TimKippTutorials.com - December 29, 2010 // Your MySQL database login information $host = "localhost"; // Your host address to your database on your server. Usually "localhost". Check with your hosting provider $user = "myjesus2xxx"; // Your username you set up for this database on your server $pass = "xxxxxxxx"; // Your password you set up for this database on your server $db = "myjesus2_login"; // The database name that you will be connecting to // Connecting to the MySQL database mysql_connect($host, $user, $pass); mysql_select_db($db); // Checking to see if the login form has been submitted if (isset($_POST['username'])) { $username = $_POST['username']; $password = $_POST['password']; // Query to check to see if the username and password supplied match the database records $sql = "SELECT * FROM users WHERE username='".$username."' AND password='".$password."' LIMIT 1"; $res = mysql_query($sql); // If login information is correct if (mysql_num_rows($res) == 1) { echo "You have successfully logged in."; exit(); } // If login information is invalid else { echo "Invalid login information. Please return to the previous page."; exit(); } } ?>

Anyone ever had this problem before?

HI gpxxx
this code working absolutely fine for me
check your settings

Hi, i had a similar problem once and it was from the

[php]
$host = “localhost”; // Your host address to your database on your server. Usually “localhost”. Check with your hosting provider
[/php]

as it says there ‘Usually’ , i was having connectivity problems and i checked my Host and they had a different localhost. I know that’s simple but took me a week before I realised :frowning:

Yep, it is a common error. Normally you do MySQL queries in this manor:
Open connection to database.
Set database name to use.
Create Query.
Execute Query.
Pull rows of data from Query as data or associated array.
Count rows of data.
Use data.

You did the first 4 items, but, you never pulled any rows of data. So you can NOT count the rows as they do not yet exist. So, change these lines:
$res = mysql_query($sql);
// If login information is correct
if (mysql_num_rows($res) == 1) {
to these:
$res = mysql_query($sql);
$rows = mysql_fetch_assoc($res);
// If login information is correct
if (mysql_num_rows($rows) == 1) {

Should fix you up nicely… Hope that explains it and fixes it up for you…

Sponsor our Newsletter | Privacy Policy | Terms of Service