Jump to content

mysql_num_rows() expects parameter 1 to be resource, boolean given


poliosynthesis
Go to solution Solved by poliosynthesis,

Recommended Posts

Hi Guys,

I have a simple  mysql_num_rows() expects parameter 1 to be resource, boolean given error in my script, I have tried to debug it myself but I don't understand why the script doesn't think the variable "$query" isn't an integer. I am trying to create a login form for users who are already registered and I want them to be able to see instant feedback as to whether their info has been accepted or not. The users won't be redirected though.

 

 

 

testlogin.php

 

<?php 
 
$name = $_GET['name'];
$password = $_GET['password'];
    if (!$name && $password) {
echo "Error";
exit;
} 
mysql_connect("localhost" , "root" , "") or die("Issue with connection!");
mysql_select_db("testlogin");
$query = mysql_query("SELECT * FROM users WHERE Name='".$name."'");
$name = $_GET['name'];
$password = $_GET['password'];
 if(!$name && $password) {
 echo 'No name or password';
 exit();
 }
 mysql_connect("localhost","root", "");
 mysql_select_db("testlogin");
 $query = mysql_query("SELECT * FROM users WHERE Name  ='".$name."'");
 $numrows = mysql_num_rows($query);
 if($numrows !=0) {
     while($row = mysql_fetch_assoc($query)) {
 
        $dbname = $row['Username'];
$dbpassword = $row['password'];
}
if($name == $dbname && $password == $dbpassword) {
    
 
    echo "you are in!";
 
 
    }else {
 
echo "Please enter a valid username and password";
 
 
}
 
 
 
 
 
 
 
}else {
    
  
                 echo "Your name is not registered!";          
 
 
 
 
 
                 } 
?>
Link to comment
Share on other sites

Sorry Guys, 

I doubled up with my mysql_connects etc. I have since removed that, it is not the error.

 

<?php 
 
$name = $_GET['name'];
$password = $_GET['password'];
    if (!$name && $password) {
echo "Error";
exit;
} 
mysql_connect("localhost" , "root" , "") or die("Issue with connection!");
mysql_select_db("testlogin");
$query = "SELECT * FROM users WHERE Name='".$name."'";
$result = mysql_query($query);
$numrows = mysql_num_rows($query);
 
 
 if($numrows !=0) {
     while($row = mysql_fetch_assoc($query)) {
 
        $dbname = $row['Username'];
$dbpassword = $row['password'];
}
if($name == $dbname && $password == $dbpassword) {
    
 
    echo "you are in!";
 
 
    }else {
 
echo "Please enter a valid username and password";
 
 
}
 
 
 
 
 
 
 
}else {
    
  
                 echo "Your name is not registered!";          
 
 
 
 
 
                 } 
?>
Link to comment
Share on other sites

are you sure that's your actual code that you need help with? $query in the last posted code is your sql statement. $result is the value from the mysql_query(). there's no way that mysql_num_rows($query) or mysql_fetch_assoc($query) will work. you should be copy/pasting your actual code so that there isn't a run-a-round about what you are actually asking help for.

 

a SELECT query that runs without any errors, returns a result resource that you can then use in other database statements. a SELECT query that fails due to an error (connection problem, permissions problem. table/column name problems, sql syntax problem, ...)  returns a boolean false value.

 

you must always test if a step in your code works or fails before trying to use the result from that step.

 

to debug why your query is failing (assuming you are using all the correct variable names in the right places), use the following when running your queries -

$result = mysql_query($query) or die("Query failed: $query<br>Error: " . mysql_error());
Edited by mac_gyver
Link to comment
Share on other sites

  • Solution

Hi Guys,

I am very sorry to have wasted your time, it was an error based on an incorrect mysql_query I entered the wrong name of the row. If you guys would like me to post my code for future reference for others with my problem I will. Sorry again to have wasted your time.

Thanks for the help you did give me though :D 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.