If exist else..

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Qaid
Forum Commoner
Posts: 33
Joined: Wed Feb 15, 2006 12:04 pm

If exist else..

Post by Qaid »

Hi,
I run MySQL with a database with a table called toys.
The table contains data equals to 1 or 0.

My question are, is it possible to make a query that ask if the table contains 1, then echo Yes!. But if the table contain 0 then it will post No!.

Thank you very much!

Best Regards...
rubberjohn
Forum Contributor
Posts: 193
Joined: Fri Feb 25, 2005 4:03 am

Post by rubberjohn »

if that is all you need the query to do, just do a normal query where that column data = 1 if the query returns a value echo yes if not echo no

alternatively just do a query that select all the data in the table with no conditions set and test the returned results

choose which ever one suits your needs
someberry
Forum Contributor
Posts: 172
Joined: Mon Apr 11, 2005 5:16 am

Post by someberry »

Something like...

Code: Select all

$rs = mysql_query("SELECT * FROM `toys` WHERE `your_field` = 1") or die(mysql_error());

echo(mysql_num_rows($rs) > 0 ? "Yes" : "No");
Qaid
Forum Commoner
Posts: 33
Joined: Wed Feb 15, 2006 12:04 pm

Post by Qaid »

1.
$rs = mysql_query("SELECT * FROM `toys` WHERE `your_field` = 1") or die(mysql_error());
2.

3.
echo(mysql_num_rows($rs) > 0 ? "Yes" : "No");
This will post "Yes" with the data 1, 0 or empty...

Help? :cry:
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

someberry wrote:Something like...

Code: Select all

$rs = mysql_query("SELECT * FROM `toys` WHERE `your_field` = 1") or die(mysql_error());

echo(mysql_num_rows($rs) > 0 ? "Yes" : "No");
more like

Code: Select all

$rs = mysql_query("SELECT `your_field` FROM `toys`") or die(mysql_error());

while ($row = mysql_fetch_assoc($result)) {
   echo ($row['your_field'] == 1 ? 'Yes' : 'No');
}
Post Reply