Results 1 to 3 of 3

Thread: Query Question

  1. #1
    Join Date
    Nov 2006
    Location
    New York
    Posts
    26
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Query Question

    How do you redirect the PHP to do something else when it returns an empty set?
    PHP Code:
    $query mysql_query("SELECT * FROM usrinfo WHERE email = '$email'") or die("died"); 
    while(
    $u=mysql_fetch_array($query)) { 

  2. #2
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    Several ways. I'm not sure of the 'official' way.
    But... here are the options.
    PHP Code:
    <?php
    //1
    while ($u=mysql_fetch_array($q)) {
    $check 1;
    dostuff();
    }
    if (
    $check != 1) {
    doerror();
    }


    //2
    if (!mysql_fetch_assoc($q)) {
    doerror();
    }
    else while (...) {}


    //3
    if (mysql_num_rows($q) == 0) {
    doerror();
    }
    else while (...) {}

    ?>
    Very simple examples, but you can apply them how you need.

    Not sure which one is best.

    It depends on the situation. It is always easier if you are returning a single piece of data, because you can then use an if statement instead of while. But if you are returning a bunch of data, which seems likely, I'm not sure what is best. Using mysql_num_rows() might be best because it's a quick check then isn't repeated each time through the loop like the $check variable method. But that one is easy, I suppose.

    Note: the use of else above seems a bit awkward, and isn't really needed, since it wouldn't do the loop because the while condition is never true. But at least the else is a faster way for the processor to avoid running the mysql_ function.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  3. #3
    Join Date
    Nov 2006
    Location
    New York
    Posts
    26
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default

    #1 worked perfect, thanks...

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
  •