This question already has an answer here:
On line 25 in my code, I wrote this:
$check = mysql_num_rows($u_check);
and it gave me the error:
Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given in D:\Download\htdocs\hoping.php on line 25
Here is my entire code of the file:
<?php include ("./header.inc.php"); ?>
<?php
$reg = @$_POST['reg'];
$fn = "";
$ln = "";
$un = "";
$em = "";
$em2 = "";
$pswd = "";
$pswd2 = "";
$d = "";
$u_check = "";
$fn = strip_tags(@$_POST['fname']);
$ln = strip_tags(@$_POST['lname']);
$un = strip_tags(@$_POST['username']);
$em = strip_tags(@$_POST['email']);
$em2 = strip_tags(@$_POST['email2']);
$pswd = strip_tags(@$_POST['password']);
$pswd2 = strip_tags(@$_POST['password2']);
$d = date("Y-m-d");
if($reg){
if($em==$em2){
$u_check = mysql_query("SELECT username FROM test WHERE username='$un'");
$check = mysql_num_rows($u_check);
if($check == 0){
if($fn&&$ln&&$un&&$em&&$em2&&$pswd&&$pswd2){
if($pswd==$pswd2){
if(strlen($un)>25||strlen($fn)>25||strlen($ln)>25){
echo "The maximum limit for username/first name/last name is 25 characters!";
}
else
{
if(strlen($pswd)>30||strlen($pswd)<5){
echo "Your password must be between 5 and 30 characters long!";
}
else
{
$pswd =md5($pswd);
$pswd2 = md5($pswd2);
$query = mysql_query("INSERT INTO test VALUES ('', '$un', '$fn', '$ln','$em', '$pswd', '$d','0')");
die("<h2>Welcome to communicate</h2>Login to your account to get started ...");
}
}
}
else {
echo "Your passwords don't match!";
}
}
else {
echo "Please fill in all of the fields";
}
}
else {
echo "Username already taken ...";
}
}
else {
echo "Your E-mails don't match!";
}
}
if(isset($_POST["user_login"]) && isset($_POST["password_login"])) {
$user_login = preg_replace('#[^A-Za-z0-9]#i', '', $_POST["user_login"]);
$password_login = preg_replace('#[^A-Za-z0-9]#i', '', $_POST["password _login"]);
}
?>
<td width="40%">
<h2>Sign Up Below!</h2>
<form action="#" method="POST">
<input type="text" name="fname" size="25" placeholder="First Name" /><p />
<input type="text" name="lname" size="25" placeholder="Last Name"/><br /><br />
<input type="text" name="username" size="25" placeholder="Username"/><br /><br />
<input type="text" name="email" size="25" placeholder="Email Address"/><br /><br />
<input type="text" name="email2" size="25" placeholder="Email Address (again)"/><br /><br />
<input type="text" name="password" size="25" placeholder="Password"/><br /><br />
<input type="text" name="password2" size="25" placeholder="Password (again)"/><br /><br />
<input type="submit" name="reg" value="Sign Up!">
</td>
</tr>
</table>
<?php include ("./footer.inc.php"); ?>
Why is it giving me that error?
</div>
mysql_query returns false on error (a boolean), so maybe that is what is happening. Try and print the error if so? For example:
$u_check = mysql_query("SELECT username FROM test WHERE username='$un'");
if(!$u_check) {
die('Invalid query: ' . mysql_error());
}
That may point you in the right direction. Also note mysql_query and related functions are long deprecated. You should learn how to use the mysqli* functions.
I don't see your mysql_connect
before executing your query, is it inside header.inc.php
?
Make sure you also include error checking with your queries and connection so that you will know why things are not working as expected.
$link = mysql_connect('localhost', 'mysql_user', 'mysql_password');
if (!$link) {
die('Could not connect: ' . mysql_error());
}
It seems that the error you mention is occurring because the query fails, there is no resource as the error says, so add error checking to your query and exit if it fails.
$u_check = mysql_query("SELECT `username` FROM `test` WHERE `username` = '$un'");
if (!$u_check) {
die('Invalid query: ' . mysql_error());
}
$check = mysql_num_rows($u_check);