oci_fetch_all 等价于 pdo?

我正在尝试使用 fetchAll() 函数从 postgres 数据库中获取结果。但是我必须从 Oracle DB 获得相同的结果,并且它不适用于 fetchAll() 函数。

使用 fetchall() 我有一个带索引号的数组,每个数组代表数据库中的一行,但是使用 oci_fetch_all 时,每个数组都有一个以列名作为索引的数组,每个数组代表数据库中的一列,那就是我需要什么

Postgres:

    $stmt = $db->prepare($req);

   $stmt->execute();
   
   $res = $stmt->fetchAll();

输出:

数组(4){

  [0]=>
  array(14) {
    ["PLU_PRESCRIPTION_S"]=>
    string(18) "PLU_PRESCRIPTION_S"
    [0]=>
    string(18) "PLU_PRESCRIPTION_S"
    ["Critère"]=>
    string(6) "02_PPA"
    [1]=>
    string(6) "02_PPA"
    ["type_prescription"]=>
    string(6) "risque"
    [2]=>
    string(6) "risque"
    ["Aire parcelle (m²)"]=>
    string(6) "298.62"
    [3]=>
    string(6) "298.62"
    ["Aire de lintersection (m²)"]=>
    string(6) "298.62"
    [4]=>
    string(6) "298.62"
    ["%"]=>
    string(6) "100.00"
    [5]=>
    string(6) "100.00"
    ["trig_comm"]=>
    NULL
    [6]=>
    NULL
  }
  [1]=>
  array(14) {
    ["PLU_PRESCRIPTION_S"]=>
    string(18) "PLU_PRESCRIPTION_S"
    [0]=>
    string(18) "PLU_PRESCRIPTION_S"
    ["Critère"]=>
    string(8) "04_zone1"
    [1]=>
    string(8) "04_zone1"
    ["type_prescription"]=>
    string(3) "PDU"
    [2]=>
    string(3) "PDU"
    ["Aire parcelle (m²)"]=>
    string(6) "298.62"
    [3]=>
    string(6) "298.62"
    ["Aire de lintersection (m²)"]=>
    string(6) "298.62"
    [4]=>
    string(6) "298.62"
    ["%"]=>
    string(6) "100.00"
    [5]=>
    string(6) "100.00"
    ["trig_comm"]=>
    NULL
    [6]=>
    NULL
  }
  [2]=>
  array(14) {
    ["PLU_PRESCRIPTION_S"]=>
    string(18) "PLU_PRESCRIPTION_S"
    [0]=>
    string(18) "PLU_PRESCRIPTION_S"
    ["Critère"]=>
    string(5) "07_MH"
    [1]=>
    string(5) "07_MH"
    ["type_prescription"]=>
    string(10) "patrimoine"
    [2]=>
    string(10) "patrimoine"
    ["Aire parcelle (m²)"]=>
    string(6) "298.62"
    [3]=>
    string(6) "298.62"
    ["Aire de lintersection (m²)"]=>
    string(6) "298.62"
    [4]=>
    string(6) "298.62"
    ["%"]=>
    string(6) "100.00"
    [5]=>
    string(6) "100.00"
    ["trig_comm"]=>
    NULL
    [6]=>
    NULL
  }
  [3]=>
  array(14) {
    ["PLU_PRESCRIPTION_S"]=>
    string(18) "PLU_PRESCRIPTION_S"
    [0]=>
    string(18) "PLU_PRESCRIPTION_S"
    ["Critère"]=>
    string(20) "18_OAP_communautaire"
    [1]=>
    string(20) "18_OAP_communautaire"
    ["type_prescription"]=>
    string(3) "OAP"
    [2]=>
    string(3) "OAP"
    ["Aire parcelle (m²)"]=>
    string(6) "298.62"
    [3]=>
    string(6) "298.62"
    ["Aire de lintersection (m²)"]=>
    string(6) "298.62"
    [4]=>
    string(6) "298.62"
    ["%"]=>
    string(6) "100.00"
    [5]=>
    string(6) "100.00"
    ["trig_comm"]=>
    string(3) "CEN"
    [6]=>
    string(3) "CEN"
  }
}

甲骨文:

  if (oci_execute($ora_req)) {
        // récupération des infos dans la base de donnée 
        $nrows = oci_fetch_all($ora_req,$res);
        //  Stockage des infos dans le tableau 
        array_push($arrPrescriptionS,$res);
    }

输出:

array(0) { }

array(7) {
  ["'PLU_PRESCRIPTION_S'"]=>
  array(4) {
    [0]=>
    string(18) "PLU_PRESCRIPTION_S"
    [1]=>
    string(18) "PLU_PRESCRIPTION_S"
    [2]=>
    string(18) "PLU_PRESCRIPTION_S"
    [3]=>
    string(18) "PLU_PRESCRIPTION_S"
  }
  ["Critère"]=>
  array(4) {
    [0]=>
    string(6) "02_PPA"
    [1]=>
    string(8) "04_zone1"
    [2]=>
    string(5) "07_MH"
    [3]=>
    string(20) "18_OAP_communautaire"
  }
  ["TYPE_PRESCRIPTION"]=>
  array(4) {
    [0]=>
    string(2) "02"
    [1]=>
    string(2) "04"
    [2]=>
    string(2) "07"
    [3]=>
    string(2) "18"
  }
  ["Aire parcelle (m²)"]=>
  array(4) {
    [0]=>
    string(6) "298,62"
    [1]=>
    string(6) "298,62"
    [2]=>
    string(6) "298,62"
    [3]=>
    string(6) "298,62"
  }
  ["Aire de l'intersection (m²)"]=>
  array(4) {
    [0]=>
    string(6) "298,62"
  }
  ["%"]=>
  array(4) {
    [0]=>
    string(3) "100"
    [1]=>
    string(3) "100"
    [2]=>
    string(3) "100"
    [3]=>
    string(3) "100"
  }
  ["TRIG_COMM"]=>
  array(4) {
    [0]=>
    NULL
    [1]=>
    NULL
    [2]=>
    NULL
    [3]=>
    string(3) "CEN"
  }
}

谢谢

zchng 回答:oci_fetch_all 等价于 pdo?

这在 PDO 中是​​不可能的。 PDO 不返回按列名索引的结果。这种行为在编写代码时不是很直观,所以最好避免。使用正常的获取模式,按行对结果进行排序。

如果您需要编写一些向后兼容的 shim,您可以使用嵌套循环自行完成。

$s = $pdo->query("SELECT * FROM bar");

$allByColumns = [];
foreach ($s->fetchAll(PDO::FETCH_ASSOC) as $index => $row) {
    foreach ($row as $column => $value) {
        $allByColumns[$column][$index] = $value;
    }
}
var_dump($allByColumns);
本文链接:https://www.f2er.com/10086.html

大家都在问