handling a SELECT that returns nothing
| From: | Tim McGeary | Date: | Fri, 27 Apr 2007 15:56:04 +0000 |
| Subject: | handling a SELECT that returns nothing | ||
| Groups: | php.db | ||
| Request: | Send a blank email to php-db+get-43495@lists.php.net to get a copy of this message | ||
I am writing a web php application that is processing a request form. There are multiple times when I need to check if a record exists (i.e. user record) and if so, check the inputted data against the data already in the database and confirm whether the inputted data should update the database.
I am getting stuck in my check of whether a record yet exists. I thought that $result in the code snippet below would be empty, so I thought I could use the empty() function in an if conditional. But a later conditional on the returned result of 0 never happened, so I echoed the $result of the mysql_query.
I have a completely empty database and my initial test should load my new record, but I am finding that the $result is being filled with
Resource id #4
from the mysql_query. Why? What does that mean? Why wouldn't it be empty? And more importantly, how do I this check properly?
Thanks,
Tim
<?php
function record_exist($select_value, $search_value, $table, $field) {
$query = "SELECT $select_value FROM $table WHERE $field = '{$search_value}'";
$result = mysql_query("SELECT $select_value FROM $table WHERE $field='{$search_value}'") or die(mysql_error());
echo "$result"."<br>";
if (empty($result)) {
return 0;} else {
return $result;} } ?>