SitePoint Sponsor

User Tag List

Results 1 to 5 of 5

Thread: Error handling

  1. #1
    SitePoint Enthusiast vaiod's Avatar
    Join Date
    Jan 2001
    Location
    Chandler, Arizona
    Posts
    81
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I am having trouble with error handling of a login script. Here is part of the script:
    ===
    $result = @mysql_query("SELECT ID, username, password FROM timeusers WHERE username='$uname' ", $dbcnx ) or die ("Invalid User Name");
    if (!$result) {
    print "Username Invalid\n";
    exit();
    }
    else {
    while ($row = mysql_fetch_array($result)) {
    if ($row["password"] == $pword) {
    print "Password OK ... Loading your data...";
    }
    else {
    print "Username or Password Incorrect";
    }
    }
    }
    ===
    If my login name is in the database it displays the correct info on the screen but if there is no corresponding entry in the database it gives me a blank screen and doesn't print anything at all. Why?

    Also what would be the easiest way to move the user to another page once their username and password match. Using header works but I don't want the page displayed if the login data is incorrect.

    Thanks

  2. #2
    SitePoint Wizard TWTCommish's Avatar
    Join Date
    Aug 1999
    Location
    Pittsburgh, PA, USA
    Posts
    3,910
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well, to put it simply, the query is successful, there's just no data to display - the error handling you have setup is only displayed if the actual query fails, and not having any rows to returned obviously is not a query failure.

    You can guard against your problem like this (I'm pretty sure mysql_num_rows is the correct function...):

    Code:
    $count = mysql_num_rows($result);
    
      if ($count < 1) {
        echo("No records returned.");
      } else {
        // Print Records With While Loop
      }
    Hope that helps.

  3. #3
    <? echo "Kick me"; ?> petesmc's Avatar
    Join Date
    Nov 2000
    Location
    Hong Kong
    Posts
    1,508
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This is what a usually do: (using your code)


    $result = @mysql_query("SELECT ID, username, password FROM timeusers WHERE username='$uname' ", $dbcnx ) or die ("Invalid User Name");
    if (!$result) {
    print "Username Invalid\n";
    exit();
    }
    else {
    while ($row = mysql_fetch_array($result)) {
    if ($row["password"] == $pword || $row["password"] != "") {
    print "Password OK ... Loading your data...";
    }
    else {
    print "Username or Password Incorrect";
    }
    }
    }


    That should work...

    Peter

  4. #4
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    or simply

    $result = @mysql_query("SELECT ID, username, password FROM timeusers WHERE username='$uname' ", $dbcnx ) or die ("Invalid User Name");

    if (mysql_num_rows($result) > 0) {
    $row = mysql_fetch_array($result);
    if($row["password"] == $pword) {
    header("Location: somepage.htm");
    }
    else {
    print "The password is wrong";
    }
    }
    else {
    print "The username does not exist";
    }
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  5. #5
    SitePoint Enthusiast vaiod's Avatar
    Join Date
    Jan 2001
    Location
    Chandler, Arizona
    Posts
    81
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Got it, Thanks all.


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
  •