Results 1 to 10 of 10

Thread: Error handling with a DB query

  1. #1

    Thread Starter
    Fanatic Member ubunreal69's Avatar
    Join Date
    Apr 2001
    Location
    Morayfield, Australia
    Posts
    609

    Error handling with a DB query

    i have a query checking my DB to see if a user exists (for my login script); i'm using the count(*) function in MySQL to display a '1' if the user exists but it cant return a '0' when it doesnt find anything, anyway, here is the code i am using:
    PHP Code:
    mysql_result(mysql_query("SELECT *, count(*) FROM users 
    WHERE uname='
    $u_name' AND upassword='$u_password
    GROUP BY uname"
    ),0,"count(*)"); 
    Anyone know how to handle this error ?

  2. #2
    PowerPoster
    Join Date
    Jul 1999
    Posts
    5,923
    Might be easier to return the records where the username matches then count the records returned with the mysql_num_rows function

  3. #3
    scoutt
    Guest
    I agree with chris on this one. don't make it harder than it is.

  4. #4
    Addicted Member TheGoldenShogun's Avatar
    Join Date
    Mar 2001
    Location
    VA/MD... anywhere around the beltway
    Posts
    236
    you could do the query with the "or die" included... like so...

    $sql1="select yadda.yadda from datta";

    $imageresult=mysql_query($sql1)
    or die ("An error has occured. <script language='javascript'>alert('uh oh, what did you do now');</script> ");

  5. #5
    scoutt
    Guest
    that "or die" is only if the it can't query the database. he waqnts it to return a "0" if he doesn't find anything.

  6. #6

    Thread Starter
    Fanatic Member ubunreal69's Avatar
    Join Date
    Apr 2001
    Location
    Morayfield, Australia
    Posts
    609
    Originally posted by chrisjk
    Might be easier to return the records where the username matches then count the records returned with the mysql_num_rows function
    hmmm, sounds good "chrisjk", i think i sorta know what u mean but not totally, could u explain a little better pls ?

    btw. "TheGoldenShogun", i'v tried a "OR DIE" thiny, doesnt solve my problem, as scoutt sais, i think its only for a seruious problem like not being able to actually access the DB. (i think i know what i mean by that )

  7. #7
    PowerPoster
    Join Date
    Jul 1999
    Posts
    5,923
    okay, here's an example
    PHP Code:
    // connect to database
    $db mysql_connect("localhost""username""password");
    mysql_select_db("databasename",$db);

    // Formulate the query
    $sql "SELECT * FROM users WHERE uname ='$u_name'";

    // Execute the query and put results in $result
    $result mysql_query($sql);

    // Get number of rows in $result.
    $num mysql_numrows($result);

    if ( 
    $num == ) {
        
    // 1 record, user exists. At this point you should check the password matches
    } else {
        
    // more than 1, or doesn't exist

    My name is chris BTW

  8. #8
    scoutt
    Guest
    or

    PHP Code:
    // connect to database
    $db mysql_connect("localhost""username""password");
    mysql_select_db("databasename",$db);

    // Formulate the query
    $sql "SELECT * FROM users WHERE uname ='$u_name'";

    // Execute the query and put results in $result
    $result mysql_query($sql);

    // Get number of rows in $result.
    $num mysql_numrows($result);

    if ( 
    $num == ) {
        
    // say they don't exist and do what you want 
    } else {
    //  if a record exists. then check for the password and then run rest of the page. so you can query the DB from here to check.



  9. #9

    Thread Starter
    Fanatic Member ubunreal69's Avatar
    Join Date
    Apr 2001
    Location
    Morayfield, Australia
    Posts
    609
    ok, thanx Scoutt & *ahem* Chris

    maybe i'll buy u 2 a beer @ the end of the year when i turn 18, then maybe air mail it to you, ****e, u don like flat beer do ya ?

    thanx again

  10. #10
    PowerPoster
    Join Date
    Jul 1999
    Posts
    5,923
    no worries

    if it's beer, I don't mind how it gets here

Posting Permissions

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



Click Here to Expand Forum to Full Width