SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Enthusiast
    Join Date
    Oct 2001
    Location
    New Zealand
    Posts
    44
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Warning message with user authentication script

    I got this code from dev cough.. Devarticles cough. It all works fine except I get this warning message:

    Warning: Unable to jump to row 0 on MySQL result index 2 in c:\inetpub\wwwroot\login.php on line 11


    quite new to php so not sure whats causing it, but Im real sure one of you good people will enlighten me [img]images/smilies/yawnb.gif[/img]

    Heres the code
    PHP Code:
    <?PHP 
    $db 
    mysql_connect('localhost''Ant_the_mus''pass') or die("Couldn't connect to the database." ); 
    mysql_select_db('Jokes') or die("Couldn't select the database" ); 
    // Add slashes to the username, and make a md5 checksum of the password. 
    $_POST['user'] = addslashes($_POST['user']); 
    $_POST['pass'] = md5($_POST['pass']); 
    $result mysql_query("SELECT userId FROM users WHERE userPass='$_POST[pass]' AND userName='$_POST[user]'" ) or die("Couldn't query the user-database." ); 
    $num mysql_result($result,0); 
    if (!
    $num) { 
    // When the query didn't return anything, 
    // display the login form. 
    echo "<h3>User Login</h3> 
    <form action='
    $_SERVER[PHP_SELF]' method='post'> 
    Username: <input type='text' name='user'><br> 
    Password: <input type='password' name='pass'><br><br> 
    <input type='submit' value='Login'> 
    </form>"

    } else { 
    // Start the login session 
    session_start(); 
    // We've already added slashes and MD5'd the password 
    $_SESSION['user'] = $_POST['user']; 
    $_SESSION['pass'] = $_POST['pass']; 
    // All output text below this line will be displayed 
    // to the users that are authenticated. Since no text 
    // has been output yet, you could also use redirect 
    // the user to the next page using the header() function. 
    // header('Location: page2.php'); 
    echo "<h1>Congratulations</h1>"
    echo 
    "You're now logged in. Try visiting <a href="page2.php">Page 2</a>."

    ?>

  2. #2
    Making a better wheel silver trophy DR_LaRRY_PEpPeR's Avatar
    Join Date
    Jul 2001
    Location
    Missouri
    Posts
    3,428
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    that would mean the query didn't return any rows and therefore there is no row 0 in in the mysql_result() line. i'd change the query line and the if() to this

    PHP Code:
    <?php

    $result 
    mysql_query("SELECT 1 FROM users WHERE userPass='$_POST[pass]' AND userName='$_POST[user]'" )
        or die(
    "Couldn't query the user-database."); 

    if (!
    mysql_num_rows($result)) {

    ?>
    - Matt ** Ignore old signature for now... **
    Dr.BB - Highly optimized to be 2-3x faster than the "Big 3."
    "Do not enclose numeric values in quotes -- that is very non-standard and will only work on MySQL." - MattR

  3. #3
    SitePoint Enthusiast
    Join Date
    Oct 2001
    Location
    New Zealand
    Posts
    44
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hey thanks for that fix DR_LaRRY_PEpPeR. I appreciate it! Though Id be even more gratefull if you wouldnt mind sharing with me some explanation so I might learn something from it.

    especially the select 1 from users which is something Ive not seen before and the mysql_num_rows function

    Cheers


  4. #4
    Making a better wheel silver trophy DR_LaRRY_PEpPeR's Avatar
    Join Date
    Jul 2001
    Location
    Missouri
    Posts
    3,428
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    sure, always good to know how things work.

    i just changed the query to SELECT 1 because all you're wanting to do is see if there's a row that matches the WHERE. you don't really need to SELECT the userID for any reason. writing 1 is just the simplest thing to put for checking if a row exists.

    the mysql_num_rows() then simply checks to see if a row was returned. it will return 0 and therefore be false if no matching row was found.

    does that explain it?

  5. #5
    SitePoint Enthusiast
    Join Date
    Oct 2001
    Location
    New Zealand
    Posts
    44
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sure does,

    thanks again


Bookmarks

Posting Permissions

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