different result multiple query with rand boolean mysql and php -
i need give result differs:
- you can eat sandwich.
- you can eat sandwich cannot eat donuts.
- you can eat donuts , drink.
the whole thing must pick @ random.
the problem result.
first had make mysql database boolean table consist of
+-------------------------------------+ | ids|name | sandwich | donuts| drink| | 1 | josh| 1 | 1 | 1 | | 2 | john| 0 | 1 | 0 | | 3 | mike| 1 | 0 | 0 | | 4 | mic | 0 | 1 | 1 | +=====================================+
i had written code in php seem first goal ok second 1 had problem. problem second one. cannot eat sandwich @ same time eat donut.. result second 1 shows list of people eat donut , not want.
is there problem code?..
here php code had done..
<?php $host="localhost"; // host name $username="root"; // mysql username $password=""; // mysql password $db_name="test1"; // database name $tbl_name="skill"; // table name // connect server , select database. mysql_connect("$host", "$username", "$password")or die("cannot connect"); mysql_select_db("$db_name")or die("cannot select db"); $sql="select ids, name skill sandwich='1' order rand() limit 3"; $result=mysql_query($sql); ?> eat sandwich <? while($rows=mysql_fetch_assoc($result)){ echo $rows['name']."<br>";}?> <br> eat donut <? $sql1="select ids, name skill donuts='1' order rand() limit 3"; $result1=mysql_query($sql1); ?> <? while($rows1=mysql_fetch_assoc($result1)){ if ($result <> $result1) {echo $rows1['name'];} else {echo nono;} }?>
i don't know you're doing while
loop. express restrictions where
clause if it's verbose, like:
select ... users (sandwich='1' , donuts!='1') or (sandwich='0') order rand()
any other restrictions on rows select can layered in there.
as note, unless absolutely must, not use mysql_query
in new code. it's being removed php because it's terribly out of date , unsafe when used incorrectly.
Comments
Post a Comment