Search code examples
phpsqlodbc

Error display multiple table select with odbc_connect


I have a query that I build based on the number of filled search fields.

"SELECT * FROM 
        (
            SELECT 
                as numero_commande,
                as code_article,
                as mpn,
                as code_cadence,
                as code_fournisseur,
                as nom_fournisseur,
                as date_besoin,
                ROW_NUMBER() OVER (ORDER BY  desc) AS Rank
            FROM
                amflib6. as POI
                JOIN  as V on  = 
                JOIN  as O on  = 
            WHERE " . implode(" AND ", $parametres) . "
        ) X WHERE Rank BETWEEN " . $premier . " AND " . $deuxieme;

When I run the query locally with the query below, I get all the fields correctly.

$odbcConnect = new PDO("odbc:$dsn", "$userid", "$password");

But the production server is on linux and the PDO request does not work anymore. So I use odbc_connect.

$odbcConnect = odbc_connect("Driver={IBM i Access ODBC Driver}; system=$mapics_server; uid=$mapics_username; pwd=$mapics_password", "Nothing", "Nothing");

Then, I wanted to make a prepared query but I couldn't find anything that worked. So I mixed the prepared query and the classic query.

        $rows = array();
        $stmt `= odbc_prepare($odbcConnect, $queryPrepare);
        if (odbc_execute($stmt, $dataParams)) {
            $result = odbc_exec($odbcConnect, $query);
            while($row = odbc_fetch_array($result)) {
                dump($row);
                $rows[] = $row;
            }
        } else {
            dump("AHHHHHHHHHHHHH");
            return [];
        }
        odbc_close($odbcConnect);

I get the data in array and return.

        $arrayObject = array(); 
        foreach($rows as $row) {
            $line = new Line();
            $line->setNumeroCommande($row["NUMERO_COMMANDE"]);
            $line->setCodeArticle($row["CODE_ARTICLE"]);
            $line->setMpn($row["MPN"]);
            $line->setCodeCadence($row["CODE_CADENCE"]);
            $line->setCodeFournisseur($row["CODE_FOURNISSEUR"]);
            $line->setNomFournisseur($row["NOM_FOURNISSEUR"]);
            $line->setDateBesoin($row["DATE_BESOIN"]);

            $date_besoin = $line->getDateBesoin();
            $annee = '20' . substr($date_besoin, -6, 2);
            $mois = substr($date_besoin, -4, 2);
            $jour = substr($date_besoin, -2, 2);
            $new_date_besoin = $jour . '/' . $mois . '/' . $annee;
            $line->setDateBesoin($new_date_besoin);
            $arrayObject[] = $line;
        } 
        dump($arrayObject);
        return $arrayObject;

the dump displays the right data except for the 2 columns that display weird results.

^ array:8 [▼
  "NUMERO_COMMANDE" => ""
  "CODE_ARTICLE" => ""
  "MPN" => ""
  "CODE_CADENCE" => ""
  "CODE_FOURNISSEUR" => b"õððùô÷"
  "NOM_FOURNISSEUR" => b"ı├┴┬@Ã┘ÍõÎ@ãÖüòâà@Ô┴Ô@õÔ─"
  "DATE_BESOIN" => ""
  "RANK" => ""
]

I'm trying to display the data from an sql query. Everything works normally locally but when I put the site in production and do a query with odbc_connect instead of PDO, the query doesn't retrieve 2 fields from the query and displays weird data while the other fields are retrieved fine.


Solution

  • Try another type of connection :

    $db = new PDO("odbc:MyDSN");