Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\registration\r­egistration.php on line 66

i already tried mysqli_error() and does not fix this problem

<html>
    <head>
        <title>Registration Form</title>
    </head>

<body>
<form method='post' action='registration.php'>
    <table width='400' border='5' align='center'>

        <tr>
        <td><h1>Registration Form</h1></td>

    </tr>

    <tr>
        <td>User Name:</td>
        <td><input type='text' name='name'/></td>
    </tr>

    <tr>
        <td>Password:</td>
        <td><input type='password' name='pass'/></td>
    </tr>
    <tr>
        <td>Email:</td>
        <td><input type='text' name='email'/></td>
    </tr>
    <tr>
        <td><input type='submit' name='submit' value='Sign Up'/></td>

    </tr>

    </table>
</form>  
</body>
</html>
<?php
mysql_connect("localhost","root","");
mysql_select_db("user_db"); 
    if(isset($_POST['submit'])){

    $user_name = $_POST['name'];
    $user_pass = $_POST['pass'];
    $user_email = $_POST['email'];

    if($user_name==''){
    echo "<script>alert('Please enter your Username')</script>";
    exit();
    }

    if($user_pass==''){
    echo "<script>alert('Please enter your password')</script>";
    exit();
    }

    if($user_email==''){
    echo "<script>alert('Please enter your email')</script>";
    exit();
    }

    $check_email="select *select* from users where 
    user_email='$user_email'";

    $run = mysql_query($check_email);

    if(mysql_num_rows($run)>0){

    echo"<script>alert('Email $user_email 
    is already exist in our databse, please try another 
    one')</script>";
    exit();
    }

    $query = "insert into users 
    (user_name,user_pass,user_email) values('
    $user_name','$user_pass','$user_email')";
    if(mysql_query($query)){

    echo "<script>alert('Registration 
    Successfull!')</script>";
    }

}



?>

Dani AI

Generated

The warning means the SELECT failed and mysql_query() returned false, then mysql_num_rows() was called with that boolean instead of a result resource. mysql_query() returns a resource for successful SELECTs or false on error, and mysql_num_rows() needs a resource — passing false produces the exact warning shown. (php.net)

Common causes (seen in the thread) are SQL syntax errors (the stray *select* spotted), wrong column/table names (the posts show inconsistent user_name vs users_name), failure to select the database, or calling the same query twice and testing the wrong return. Capture the real error text immediately after the failing query with mysql_error() / mysql_errno() and confirm mysql_select_db() returned true; that will show whether the problem is syntax, permissions, or a missing DB. was right that the error output is critical to diagnose. (php.net)

Longer‑term: stop using the old mysql_* API (deprecated in PHP 5.5, removed in PHP 7.0) and move to mysqli or PDO with prepared statements for safety and reliable error handling. Prepared statements avoid manual escaping and make these checks simpler. (php.net)

Example (mysqli prepared statements — use as a template):

<?php
$mysqli = new mysqli('localhost','root','','user_db');
if ($mysqli->connect_errno) { die('Connect error: '.$mysqli->connect_error); }

$stmt = $mysqli->prepare('SELECT 1 FROM users WHERE user_email = ? LIMIT 1');
$stmt->bind_param('s',$user_email);
$stmt->execute();
$stmt->store_result();
if ($stmt->num_rows) { /* email exists */ }
else {
  $stmt->close();
  $ins = $mysqli->prepare('INSERT INTO users (user_name,user_pass,user_email) VALUES (?,?,?)');
  $ins->bind_param('sss',$user_name,$user_pass,$user_email);
  $ins->execute();
  if ($ins->affected_rows === 1) { /* success */ }
  $ins->close();
}
$mysqli->close();
?>

If staying with legacy mysql_* for now, call the query once, store its return, check it, and log mysql_error() immediately when it returns false.

Recommended Answers

All 20 Replies

$check_email="select *select* from users where user_email='$user_email'";

What is *select* doing there?

i tried deleting it but it does not solve the error

Try select * from ...

still the same error

Using mysqli_error () won't solve the problem. Rather, it'll aid in diagnosing it. What is the output received when you kill on mysqli_error? The PHP error you're receiving is due to a bad query, so the output of mysqli_error is critical.

You're using the mysql extension, so you should be using mysql_error() (without the i).

$check_email = "select * from users where user_email='$user_email'";
$run = mysql_query($check_email) or die(mysql_error());

It says no database selected

I solve thr problem but it wont echo out "Successfull"

Add the same error check to the second query.

Which second query?

Line 77.

I added or die(msql_error()); and it does not work it gave me error

it does not work it gave me error

Specify.

Specify?

I can't guess what you did, so show the code you used and show the error message.

Off-topic: read this.

`

if(mysql_query($query)) {

    echo "<script>alert('Registration 
    Successfull!')</script>";
    or
    die(mysql_error());

`

$result = mysql_query($query) or die(mysql_error());
if ($result)
{
    echo "<script>alert('Registration Successfull!')</script>";
}

it works sometimes only

$query = "insert into users 
    (users_name,users_pass,users_email) values('
    $users_name','$users_pass','$users_email')";

if(mysql_query($query)) {
    $result = mysql_query($query) or die(mysql_error());
    if ($result)
{
    echo "<script>alert('Registration Successfull!')</script>";
}

it works sometimes only

Your code above is not what I showed you.

Where should i put it?

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.