0

Hello every body is it possible to pass an array containing some data to a function and put this array of data in a WHERE condition of a mysql query? Please, take a look to my code if it's correct. Anyway it is not working at the moment, it prints "No results" ...

public function target_query($ids_array){

    include_once 'connection.php';
    // Create connection
    $conn = new mysqli($servername, $username, $password, $dbname);
    // Check connection
    if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
    }

    $ids = join("','",$ids_array);  

    $sql = "SELECT codice_target FROM customer WHERE id_customer IN ('$ids')";
    $result = $conn->query($sql);
    $arraytoclass = array();

    if ($result->num_rows > 0) {
    // output data of each row
    //echo "tutto ok";
        while($row = $result->fetch_row()) {
        //echo "Codice target: " . $row["codice_target"]."<br>";
        $arraytoclass[] = $row;
        //echo "codice target:".$arraytoclass[$i]['codice_target']; 

        }
        //print_r($arraytoclass);
        return $arraytoclass;
        //print_r($arraytoclass);

    } else {
        echo "NO results";
    }

    return $arraytoclass;
    $conn->close();

} 

The result of the query will be passed inside another function here below:

        public function fputToFile($file, $allexportfields, $object, $ae)
        {

            if($allexportfields && $file && $object && $ae)
            {
                //one ready for export product
                $readyForExport = array();


                //put in correct sort order
                foreach ($allexportfields as $value)
                {
                    $object = $this->processDecimalSettings($object, $ae, $value);
                    $readyForExport[$value] = iconv("UTF-8", $ae->charset, $object[$value]);

                }

                $arraytoclass = $this->target_query($readyForExport['id_customer']);
                print_r ($arraytoclass);
// and so on...
// and so on...

Many thanks in advance.

Darius Pum
  • 29
  • 5
  • See if you have some hidden errors first. So put this two lines as the first ones in your method: `error_reporting(E_ALL); ini_set('display_errors', 1);`. And these two before the `$conn=...` line: `$mysqliDriver = new mysqli_driver(); $mysqliDriver->report_mode = (MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);`. Any errors shown on screen? – PajuranCodes Mar 29 '18 at 00:40
  • This appears to be a duplicate question: https://stackoverflow.com/questions/907806/passing-an-array-to-a-query-using-a-where-clause – desoares Mar 29 '18 at 01:20
  • Many thanks Dakis I'm going to paste the warning messages: `Notice: Undefined index: codice_target in /var/www/unisit_ecommerce/modules/advancedexport/advancedexport.php on line 1371` `Warning: join(): Invalid arguments passed in /var/www/unisit_ecommerce/modules/advancedexport/advancedexport.php on line 1287` – Darius Pum Mar 29 '18 at 07:37
  • The `Notice` tells you, that you are trying to access an array item by a non-existent key named `codice_target`. Probably something like that `$arraytoclass[$i]['codice_target']` is the problem, since `$i` is not defined nowhere. The `Warning` tells you that something is wrong with `$ids_array`. It must be an array! – PajuranCodes Mar 29 '18 at 12:55
  • What you are trying to do is possible and you are doing it right. But, actually, your code should use [prepared statements](https://secure.php.net/manual/en/mysqli.quickstart.prepared-statements.php), in order to avoid [SQL injection](https://en.wikipedia.org/wiki/SQL_injection). For this, mysqli has limitations, especially related with passing variable lengthed arrays into sql statements. So, my strong advice is to use the [PDO](https://secure.php.net/manual/en/intro.pdo.php) extension instead of mysqli. – PajuranCodes Mar 29 '18 at 13:06

0 Answers0