Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New Coder
    Join Date
    Apr 2012
    Posts
    67
    Thanks
    12
    Thanked 1 Time in 1 Post

    Question can you see where my code is wrong?

    hi guys i am making a log in form and i have noticed that when i come to use the form i get an error. Here is the code and the error i receive, do you have any ideas on how i can fix this? thanks calsta
    PHP Code:
    <?php
    $host
    ="localhost"// Host name
    $username="root"// Mysql username
    $password=""// Mysql password
    $db_name="test"// Database name
    $tbl_name="members"// Table name

    // Connect to server and select database.
    mysql_connect("$host""$username""$password")or
    die(
    "cannot connect");
    mysql_select_db("$db_name")or die("cannot select DB");

    // username and password sent from form
    $myusername=$_POST['myusername'];
    $mypassword=$_POST['mypassword'];

    // To protect MySQL injection (more detail about MySQL injection)
    $myusername stripslashes($myusername);
    $mypassword stripslashes($mypassword);
    $myusername mysql_real_escape_string($myusername);
    $mypassword mysql_real_escape_string($mypassword);

    $sql="SELECT * FROM $tbl_name WHERE username='$myusername' and password='$mypassword'";
    $result=mysql_query($sql);

    $count=mysql_num_rows($result);

    if(
    $count==1){
    session_register("myusername");
    session_register("mypassword");
    header("location:login_success.php");
    }
    else {
    echo 
    "Wrong Username or Password";
    }
    ?>
    error is: Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given in C:\wamp\www\Login\check_login.php on line 26

  • #2
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,865
    Thanks
    160
    Thanked 2,224 Times in 2,211 Posts
    Change
    PHP Code:
    $result=mysql_query($sql); 
    to
    PHP Code:
    $result=mysql_query($sql) or die(mysql_error()); 
    to get the error in your query.
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • Users who have thanked abduraooft for this post:

    calsta (04-28-2012)

  • #3
    New Coder
    Join Date
    Apr 2012
    Posts
    67
    Thanks
    12
    Thanked 1 Time in 1 Post
    Quote Originally Posted by abduraooft View Post
    Change
    PHP Code:
    $result=mysql_query($sql); 
    to
    PHP Code:
    $result=mysql_query($sql) or die(mysql_error()); 
    to get the error in your query.
    Thanks for that i help me find out that the table it is trying to access does not exist but i don't see the part where it is trying to access test.members here is the code with the part you added in
    PHP Code:
    <?php
    $host
    ="localhost"// Host name
    $username="root"// Mysql username
    $password=""// Mysql password
    $db_name="test"// Database name
    $tbl_name="members"// Table name

    // Connect to server and select database.
    mysql_connect("$host""$username""$password")or
    die(
    "cannot connect");
    mysql_select_db("$db_name")or die("cannot select DB");

    // username and password sent from form
    $myusername=$_POST['myusername'];
    $mypassword=$_POST['mypassword'];

    // To protect MySQL injection (more detail about MySQL injection)
    $myusername stripslashes($myusername);
    $mypassword stripslashes($mypassword);
    $myusername mysql_real_escape_string($myusername);
    $mypassword mysql_real_escape_string($mypassword);

    $sql="SELECT * FROM $tbl_name WHERE username='$myusername' and password='$mypassword'";
    $result=mysql_query($sql) or die(mysql_error());  

    $count=mysql_num_rows($result);

    if(
    $count==1){
    session_register("myusername");
    session_register("mypassword");
    header("location:login_success.php");
    }
    else {
    echo 
    "Wrong Username or Password";
    }
    ?>
    from this i don't understand why it is connecting to test.members instead of just test, any ideas?


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •