Jump to content

problem in php coding in c


nairarun88

Recommended Posts

Hello,

 

i have been working on client site... looking for help from experts....

 

i got the warning error on the site... Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given in

 

C:\wamp\www\registration\login.php on line 19

 

code is bellow... kindly help me where i have did any mistake....

 

    <?php

    $username = $_POST ['username'];

    $password = $_POST ['password'];

    if ($username&&$password){

    $connect = mysql_connect("localhost","root","") or die("Couldn't connect!");

    mysql_select_db("phplogin") or die("Couldn't find db");

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

    $numrows = mysql_num_rows($query);

    echo $numrows;

    }

    else

    die("Please enter a username and password");

    ?>

Link to comment
https://forums.phpfreaks.com/topic/268354-problem-in-php-coding-in-c/
Share on other sites

Your query is failing. You need to verify that the query passed before you try to use the results. I would advise always creating your queries as string variables. Then if there is an error you can echo the query to the page to verify the contents. If you had done that in this instance the error would have been obvious.

In the query you had an opening single quote mark around the username, but no closing single quote mark. There were a lot of other potential problems

 

$username = trim($_POST['username']);
$password = trim($_POST['password']);
if ($username!='' && $password!='')
{
    $connect = mysql_connect("localhost","root","") or die("Couldn't connect!");
    mysql_select_db("phplogin") or die("Couldn't find db");
    $query = sprintf("SELECT * FROM users WHERE username='%s' AND password='%s'",
                     mysql_real_escape_string($username), 
                     mysql_real_escape_string($password));
    $result = mysql_query($query) or die(mysql_error());
    if(!$result)
    {
        die("Query: $query<br>Error: " . mysql_error());
    }
    else
    {
        $numrows = mysql_num_rows($query);
        echo $numrows;
    }
}
else
{
    die("Please enter a username and password");
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.