simple query question

simple query question

am 02.09.2007 08:23:40 von Tony Peardon

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.

Thanks,

Tony.

Re: simple query question

am 02.09.2007 08:50:58 von Shion

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

Re: simple query question

am 02.09.2007 17:23:06 von Aaron Saray

On Sep 2, 1:50 am, "J.O. Aho" 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.

Re: simple query question

am 02.09.2007 18:59:34 von Paul Lautman

Aaron Saray wrote:
> On Sep 2, 1:50 am, "J.O. Aho" 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.

You could, but why would you want to count more than one row if all you
wanted to know was whether the target exists at least once?