For Programmers: Free Programming Magazines  


Home > Archive > PHP SQL > September 2007 > simple query question









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author simple query question
Tony Peardon

2007-09-02, 4:03 am

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.
J.O. Aho

2007-09-02, 4:03 am

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
Aaron Saray

2007-09-02, 7:04 pm

On Sep 2, 1:50 am, "J.O. Aho" <u...@example.net> wrote:
> Tony Peardon wrote:
>
>
>
> 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.

Paul Lautman

2007-09-02, 7:04 pm

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


Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com