different result multiple query with rand boolean mysql and php -


i need give result differs:

  1. you can eat sandwich.
  2. you can eat sandwich cannot eat donuts.
  3. 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

Popular posts from this blog

linux - Does gcc have any options to add version info in ELF binary file? -

android - send complex objects as post php java -

charts - What graph/dashboard product is facebook using in Dashboard: PUE & WUE -