Log in

View Full Version : Resolved help with a php script



itivae
11-25-2013, 02:40 AM
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


$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;
}

traq
11-25-2013, 03:48 AM
$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 (http://php.net/mysqli_result.num_rows) is what you need to check.

itivae
11-25-2013, 04:37 PM
heh Derh..... Thanks a lot Traq. I knew it was something obvious just couldn't see it. Sorry for not posting a descriptive title.

traq
11-25-2013, 07:18 PM
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. :))