Click here to Skip to main content
15,895,554 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hey,
please can anyone help me with this error
Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in

and here is my code
<<pre lang="xml">?
$conn = mysql_connect("localhost",XXXX","XXXX");
mysql_select_db("users");
if($_POST['submit']) {
$username = $_POST['username'];
$password = $_POST['password'];
$checkUserQuery = mysql_query("SELECT * FROM users WHERE username='$username'");
if (!$username OR !$password){
echo("ERROR: Please enter a username and password");
}
elseif(mysql_num_rows($checkUserQuery) > 0) {
echo("ERROR:Username already exists");
}
else{
$query = mysql_query("INSERT INTO users VALUES ('', '$username','$password')");
echo ("Sign Up Succcessful");
}
}
?>
<form method="POST" action="">
Username: <input type="text" name="username"><br>
Password: <input type="password" name="password"<br>
<input type="submit" name="submit" value="Sign Up!!"

>

Please can anyone help me
Thanks in Advance
Posted
Comments
Joan M 4-Jul-11 12:02pm    
in those cases, it is really helpful to get the line number (all the error message) and the lines numbered or at least a reference of where the error is located... Anyway, I guess that the solution is explained in my answer below...

try this:

replace this:


$checkUserQuery = mysql_query("SELECT * FROM users WHERE username='$username'");


with:
$checkUserQuery = mysql_query("SELECT * FROM users WHERE username='".$_POST['username']."'");


or with:
$checkUserQuery = mysql_query("SELECT * FROM users WHERE username='".$username."'");


hope this helps :)

comment here for further queries!!
 
Share this answer
 
v4
You should change the line where you are making the query:

from that:
$checkUserQuery = mysql_query("SELECT * FROM users WHERE username='$username'");

to:
$checkUserQuery = mysql_query("SELECT * FROM users WHERE username=".$username);


This should help you to get the desired result...

Before doing that you were sending the text $username and not the value of the variable $username to the sql query.

Anyway, use the function
var_dump($username);
in order to see the contents of almost anything in php... It will help you a lot...

Hope this helps.

:thumbsup:
 
Share this answer
 
Comments
Seif Hatem 4-Jul-11 12:41pm    
same problem
Joan M 4-Jul-11 12:50pm    
Could you give us the return of var_dump($username); and var_dump($checkUserQuery); also... the same kind of modification must be applied into the second query... you should give us the complete error and the line numbers which are the two queries...
Uday P.Singh 4-Jul-11 13:15pm    
$checkUserQuery = mysql_query("SELECT * FROM users WHERE username=".$username);

this line will always give error. It should be like this:

$checkUserQuery = mysql_query("SELECT * FROM users WHERE username='".$username."'");
Joan M 5-Jul-11 2:50am    
I guess it depends on the PHP engine you are using... this works like charm for me in EasyPHP 5.3.5... anyway, your syntax can also be used...
Uday P.Singh 5-Jul-11 13:36pm    
I don't know about EasyPHP 5.3.5, but its giving error in PHP 5.3.0..

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900