Help, selecting data from mysql using array in WHERE clause

[php]<?php
$array = array(
‘FBR-15’,‘FBR-16’,‘FBR-17’,‘FBR-18’,‘FBR-19’
);

        $q = mysqli_query($dbc,"SELECT * FROM tree_tbl WHERE tree_code IN ('$array')");
        while($data = mysqli_fetch_array($q)){
          echo "<li>".$data['tree_code']."</li></br>";
        }
      ?>[/php]

Well, the WHERE clause will work if you prepare it correctly. It does not actually look inside of an array variable.
You can prepare it with a simple implode function…

One example would be:
[php]
$array_data = implode(",", $array);
$q = mysqli_query($dbc,“SELECT * FROM tree_tbl WHERE tree_code IN ($array_data)”);
[/php]

Or, you can combine them like this:
[php]
$q = mysqli_query($dbc,“SELECT * FROM tree_tbl WHERE tree_code IN (” . implode(",", $array) . “)”);
[/php]
Hope that helps you…

Thanks,Its works

Great! Always nice to solve a programming puzzle…

CYA in the bitstream when you find another puzzle…

Sponsor our Newsletter | Privacy Policy | Terms of Service