how to retrieve data from database?

Discussion in 'PHP' started by ankifreeze, Feb 27, 2011.

  1. #1
    hello...i have problem....I want retrieve data black blue red from database using sql In operator but I don't know how.....
    
    
    $color="black blue red";
    
    
    $query=mysql_query("SELECT * FROM table WHERE color IN ('black','blue','red' )";
    
    while($row = mysql_fetch_array($query))
    {
        echo "color :".$row['color']." number:".$row['number']."<br/>" ;
     }  ?>
    
    
    
    
    
    PHP:

     
    ankifreeze, Feb 27, 2011 IP
  2. JoelLarson

    JoelLarson Peon

    Messages:
    61
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #2
    I would imagine this to work just fine for what you want to do.

    $colors = "black blue red";
    $colors = "'".str_replace(' ', "', '", $colors)."'";
    
    $sql = sprintf("SELECT * FROM `table` WHERE `color` IN (%s);", $colors);
    $result = mysql_query($sql);
    
    while($row = mysql_fetch_assoc($query))
    {
    	echo 'Color: '.$row['color'].' Number:'.$row['number'].'<br />';
    }
    PHP:
     
    JoelLarson, Feb 28, 2011 IP