Help, selecting data from mysql using array in WHERE clause

Please I need help on solving this

<?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>";
            }
          ?>`

SQL won’t take the array as an array, it needs to be a string to be in the query.
So try:

$array = "'FBR-15','FBR-16','FBR-17','FBR-18','FBR-19'";

$q = mysqli_query($dbc," SELECT * FROM tree_tbl WHERE tree_code IN ( '".implode("', '", $array)."' )");
However, if your $array comes from a request you must sanitize first.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.