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
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");
}

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.