|
Posted by Aaron Saray on 09/02/07 15:23
On Sep 2, 1:50 am, "J.O. Aho" <u...@example.net> wrote:
> 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
You could also do this:
"select count(*) as COUNTROWS from data where id='$id' and
parent='$parent' and node='$node'"
Then, you can get the results - $row['COUNTROWS'] will be the integer
number of how many rows there are that match.
Navigation:
[Reply to this message]
|