Results 1 to 4 of 4

Thread: help with a php script

  1. #1
    Join Date
    Nov 2009
    Posts
    107
    Thanks
    7
    Thanked 2 Times in 2 Posts

    Default help with a php script

    Hi.

    I am not sure why i am having such a problem with this. I want to check a query and if it matches data in the last name column I want to print the data. However, if there is no match i want it to echo an error and end. Im sure this is probably a typo somewhere but I do not see it. Hope fully one of you will.

    Thanks

    PHP Code:
    $query "SELECT * FROM main WHERE last = '$lastname'";


     if (
    $result $mysqli->query($query)){
        while (
    $obj $result->fetch_object()) {
        
           
    printf ('<div class="name">'."%s"$obj->last);
           
    printf ("%s"$obj->first);
                 
    printf ("%s"$obj->voter_id);
                 
    printf ("%s"$obj->dob);
                 
    printf ("%s"$obj->city .'</div>');
            
                
                }
            }
            else{
            echo 
    'There is no person in your database by that last name.';
            
    $mysqli->close();
            exit;
            } 
    Last edited by itivae; 11-25-2013 at 04:44 PM.

  2. #2
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default

    $result will always be truthy unless there is an error in your query (even if there are no rows in the result). $result->num_rows is what you need to check.
    Last edited by traq; 11-25-2013 at 07:19 PM.

  3. #3
    Join Date
    Nov 2009
    Posts
    107
    Thanks
    7
    Thanked 2 Times in 2 Posts

    Default

    heh Derh..... Thanks a lot Traq. I knew it was something obvious just couldn't see it. Sorry for not posting a descriptive title.

  4. #4
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default

    Quote Originally Posted by itivae View Post
    heh Derh..... Thanks a lot Traq. I knew it was something obvious just couldn't see it. Sorry for not posting a descriptive title.
    You're welcome.

    (The "non-descriptive thread title" was actually another mod, but yeah—threads with vague, "I needz help" titles tend to get glossed over. It's a help forum, so "help" is a given; likewise, it's a php forum, so "php" is a given. Next time, just give us a clue about the actual problem you're facing. )

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
  •