1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

mysql_num_rows() expects parameter 1 to be resource, null given in login.php on line 14

Discussion in 'PHP' started by rehan581, Feb 19, 2014.

?

hi, dear all, i am new in php please help me

Poll closed Feb 26, 2014.
  1. poll

    0 vote(s)
    0.0%
  2. poll

    0 vote(s)
    0.0%
Multiple votes are allowed.
  1. #1
    <?php
    if(isset($_POST) && !empty($_POST))
    {
    session_start();
    include("config.php"); //including config.php in our file
    $username = mysql_real_escape_string(stripslashes($_POST['username'])); //Storing username in $username variable.
    $password = mysql_real_escape_string(stripslashes(md5($_POST['password']))); //Storing password in $password variable.


    $match = "select id from $table where username = '".$username."' and password = '".$password."';";

    $qry = mysql_query($match);

    $num_rows = mysql_num_rows($qry);



    if ($num_rows <= 0) {
    SEMrush
    echo "Sorry, there is no username $username with the specified password.";

    echo "Try again";

    exit;

    } else {



    $_SESSION['user']= $_POST["username"];
    header("location:home.php");
    // It is the page where you want to redirect user after login.
    }
    }else{
    ?>
    <html>
    <head>
    <title>Login</title>
    <link rel="stylesheet" type="text/css" href="style.css" />
    </head>
    <body>
    <div class="container login">
    <form action="<?php $_SERVER['PHP_SELF'] ?>" method="post" class="form-signin" id = "login_form" >
    <h2 class="form-signin-heading">Admin/Employee Login</h2>
    <input type="text" name="username" size="20" placeholder="Username">
    <input type="password" name="password" size="20" placeholder="Password"></br>
    <input type="submit" value="Log In" class="btn btn-large btn-primary">
    <a href="signup.php">Sign Up</a>
    </form>
    </div>
    </body>
    </html>
    <?php
    }
    ?>
     
    rehan581, Feb 19, 2014 IP
    SEMrush
  2. stephan2307

    stephan2307 Well-Known Member

    Messages:
    1,277
    Likes Received:
    33
    Best Answers:
    7
    Trophy Points:
    150
    #2
    ok

    1. you should NOT be using the mysql functions as they will be deprecated at some point. use mysqli or PDO instead.
    2. please use the code highlight functionality when posting code. makes it much easier to read.
    3. that error basically means that the query failed. Looking at the code that you have posted I wonder if it is the fact that you have $table instead of a tablename and I can't see $table declared anywhere. To get more details about possible errors paste the following line after the line with mysql_query
      echo mysql_error();
      Code (markup):
     
    stephan2307, Feb 19, 2014 IP
    deathshadow likes this.
  3. loop

    loop Active Member

    Messages:
    519
    Likes Received:
    4
    Best Answers:
    1
    Trophy Points:
    90
    #3
    you was forgot:

    mysql_connect("localhost", "mysql_user", "mysql_password");
    mysql_select_db("database");

    before

    $qry = mysql_query($match);

    but really should use PDO.
     
    loop, Feb 19, 2014 IP
  4. DomainerHelper

    DomainerHelper Well-Known Member

    Messages:
    445
    Likes Received:
    20
    Best Answers:
    0
    Trophy Points:
    100
    #4
    Also call you variables correctly, not related to the error but this is how it should be done. This prevents UNDEFINED VARIABLE notices.

    if (!empty($_POST['username'])) {
        $username = mysql_real_escape_string(strip_tags($_POST['username'])); //Storing username in $username variable.
    } else {
        echo 'Username cannot be blank';
        die;
    }
    Code (markup):
    Also, why do you have stripslashes($_POST['username'])? You need strip_tags(), not stripslashes().
     
    DomainerHelper, Feb 19, 2014 IP
  5. DomainerHelper

    DomainerHelper Well-Known Member

    Messages:
    445
    Likes Received:
    20
    Best Answers:
    0
    Trophy Points:
    100
    #5
    also, change this...
    $qry = mysql_query($match);
    Code (markup):
    to...
    $qry = mysql_query($match) or die(mysql_error());
    Code (markup):
    If you want to see why your query failed.
     
    DomainerHelper, Feb 19, 2014 IP
  6. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #6
    The die(mysql_error()) -function is really NOT recommended, as it outputs stuff from the mysql_-config you don't want to expose to the public if something breaks. It's good for development, but shouldn't really be used in production.
     
    PoPSiCLe, Feb 20, 2014 IP
  7. DomainerHelper

    DomainerHelper Well-Known Member

    Messages:
    445
    Likes Received:
    20
    Best Answers:
    0
    Trophy Points:
    100
    #7
    Yeah since he is in development, I told him to use it so he can see his errors live without having to bounce around checking logs.
     
    DomainerHelper, Feb 20, 2014 IP
  8. swapinbiz

    swapinbiz Member

    Messages:
    10
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    41
    #8
    you have to replace $num_rows < 1 with $num_rows <= 0 in if condition.
     
    swapinbiz, Feb 24, 2014 IP
  9. swapinbiz

    swapinbiz Member

    Messages:
    10
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    41
    #9
    and do not make separate variable $num_rows = mysql_num_rows($qry); just us mysql_num_rows($qry) within if condition.
     
    swapinbiz, Feb 24, 2014 IP
  10. DomainerHelper

    DomainerHelper Well-Known Member

    Messages:
    445
    Likes Received:
    20
    Best Answers:
    0
    Trophy Points:
    100
    #10
    You are wrong. There is nothing from with how he did that. The issue was with the query itself.
     
    DomainerHelper, Feb 24, 2014 IP
  11. Himanshu DIxit

    Himanshu DIxit Greenhorn

    Messages:
    1
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    21
    #11
    Just change the $match to

    $match = "select id from $table where username = '$username' and password = '$password'";
    and after this your program will work fine
     
    Himanshu DIxit, Mar 5, 2014 IP