Hi all,
I seem to be having a problem. I get the above error when trying my login script.

<?php

$dbhost = "localhost";
$dbname = "userlogin";
$dbuser = "adminuser";
$dbpass = "xxxxxxx";
$dbtable = "users";

mysql_connect ( $dbhost, $dbuser, $dbpass, $dbtable)or die("Could not connect: ".mysql_error());
mysql_select_db($dbname) or die(mysql_error());

session_start();

$username = $_POST[‘username’];
$password = md5($_POST[‘password’]);

$query = "SELECT * FROM users WHERE username=’$username’ AND password=’$password’";

$result = mysql_query($query);

if (mysql_num_rows($result) != 1) {
echo 'Bad Login';
    include "index.php";
} else {
    $_SESSION[‘username’] = '$username';
    include “memberspage.php”;
}

?>

It comes up on line 21.
The full error is:

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in C:\WebServerDocs\Public\login.php on line 21
Bad Login

I am using Apache 2.0 , MySQL 5.0, php 5 on windows platform.

Any help would be much appreciated.
Thanks

when setting the result change it to this, and try again

$result = mysql_query($query) or die (mysql_error());

Hi,
I have edited the code and placed the snippet that you provided in and it is returning the following error:

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '� AND password=�' at line 1

Any more ideas?

Thanks

yes, very common problem

i'll help you solve it

before executing the query, echo it and you can see whats wrong

echo $query;
mysql_connect ( $dbhost, [B]$dbname[/B], $dbuser, $dbpass, $dbtable)or die("Could not connect: ".mysql_error());

You forgot to add the db name ..
?

On the mysql_connect function i shouldnt need the $dbname in it as i have the mysql_select_db function underneath selecting the database.

I have solved the parse syntax errors. But now i am facing anothre problem.

This is the code now:

<?php

$dbhost = "localhost";
$dbname = "userlogin";
$dbuser = "adminuser";
$dbpass = "XXXXXXX";
$dbtable = "users";

mysql_connect ( $dbhost, $dbuser, $dbpass, $dbtable)or die("Could not connect: ".mysql_error());
mysql_select_db($dbname) or die(mysql_error());

session_start();

$username = $_POST[‘username’];
$password = $_POST[‘password’];

$query = "SELECT * FROM users WHERE username='$username' and password='$password' ";

$result = mysql_query($query) or die (mysql_error()); 

if (mysql_num_rows($result) != 1) {
$error = 'Failed Login';
    include 'index.php';
} else {
    $_SESSION[‘username’] = '$username';
    include 'memberspage.php';    
}

?>

The problem now is, that when the user logs in, it doesnt go to the memberspage.php it stays on the index.php page ... you can try it at http://scouseywebsites.uk.to .

Any ideas lads?
Thanks

if (mysql_num_rows($result) != 1) {
$error = 'Failed Login';
    include 'index.php';
} else {
    $_SESSION[‘username’] = '$username';
    include 'memberspage.php';    
}

in this part result should equal 1
if (mysql_num_rows($result) == 1)
that is why it runs the else operation and goes to another page and

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.