|
Posted by J.O. Aho on 09/02/07 06:50
Tony Peardon wrote:
> Hi all,
> I've got a php function which asks mysql the following question...
>
> "select * from data where id='$id' and parent='$parent' and node='$node'"
>
> Now, I don't need to "select *", because the function is simply supposed
> to return true if the data exists in the database, so how do I get the
> query to return true if the data exists, and false if it doesn't.
Use mysql:
$query="SELECT id FROM data WHERE id='{$id}' AND parent='{$parent}' AND
node='{$node}' LIMIT 1";
$res=mysql_query($query);
if(mysql_num_rows($res)) {
echo "true";
} else {
echo "false";
}
Use mysqli:
$query="SELECT id FROM data WHERE id='{$id}' AND parent='{$parent}' AND
node='{$node}' LIMIT 1";
$res=mysqli->query($query);
if($res->num_rows) {
echo "true";
} else {
echo "false";
}
num_rows gives you the amount of rows found, you get 0 if none are found,
which makes it work like false in an if-statement, otherwise you should get 1
(thanks to the limit, no point in searching the whole database for duplicate
rows), which will be as true in an if-statement.
Of course you should make some error checking before checking the number of rows.
--
//Aho
Navigation:
[Reply to this message]
|