Return SQL 结果为每列简单数组:PHP OCI

Return SQL results as simple arrays per column : PHP OCI

我在说 "array" 时可能使用了不正确的白话,但我有一个 SQL 查询,我正在执行 return 的数据,如下所示:

HR  MAILED  FAILED  BOUNCED
06  1442    0   0
07  825218  185 2433
08  210672  496 2019
09  254125  388 2854
10  159744  366 2725
11  164515  360 2413
12  1246917 547 3329
13  73837   269 1487

我想要它 return 4 个单独的数组存储为它们自己的变量,类似于:

$var1 = ["hr","mailed","failed","bounced"]
$var2 = [1442, 825218, 21062, 254125, 159744, 164515, 1246917, 73837]
$var3 = ...results from 2nd column...
$var4 = ...results from 3rd column...

我目前的代码正在吐出一个数组,但它很不稳定...

<?php
include 'includes/connect.php';
include 'includes/queries.php';

$stid = oci_parse($conn, $mfg_last6);
oci_execute($stid);
$ncols = oci_fetch_all($stid, $res, null, null, OCI_FETCHSTATEMENT_BY_COLUMN);

echo "$ncols rows fetched<br>\n";
var_dump($res);

oci_free_statement($stid);
oci_close($conn);

?>

结果是...

8 rows fetched
array(4) { ["HR"]=> array(8) { [0]=> string(2) "06" [1]=> string(2) "07" [2]=> string(2) "08" [3]=> string(2) "09" [4]=> string(2) "10" [5]=> string(2) "11" [6]=> string(2) "12" [7]=> string(2) "13" } ["MAILED"]=> array(8) { [0]=> string(4) "1442" [1]=> string(6) "825218" [2]=> string(6) "210672" [3]=> string(6) "254125" [4]=> string(6) "159744" [5]=> string(6) "164515" [6]=> string(7) "1247164" [7]=> string(6) "123649" } ["FAILED"]=> array(8) { [0]=> string(1) "0" [1]=> string(3) "185" [2]=> string(3) "496" [3]=> string(3) "388" [4]=> string(3) "366" [5]=> string(3) "360" [6]=> string(3) "547" [7]=> string(3) "446" } ["BOUNCED"]=> array(8) { [0]=> string(1) "0" [1]=> string(4) "2433" [2]=> string(4) "2019" [3]=> string(4) "2854" [4]=> string(4) "2725" [5]=> string(4) "2413" [6]=> string(4) "3329" [7]=> string(4) "2352" } }

我敢肯定它可能非常简单,但我是 PHP 的新手,我仍在学习很多东西。感谢对此的任何帮助!

谢谢!

每一列只是 $res 数组的一个元素。

$var1 = array_keys($res);
$var2 = $res['MAILED'];
$var3 = $res['FAILED'];
$var4 = $res['BOUNCED'];

您将必须手动执行 header(第一列)...然后循环遍历结果:

$final_array = ["hr","mailed","failed","bounced"];
while (($row = oci_fetch_array($stid, OCI_BOTH)) != false) {
    $final_array["hr"] = $row['hr'];
    $final_array["mailed"] = $row['mailed'];
    $final_array["failed"] = $row['failed'];
    $final_array["bounced"] = $row['bounced'];
}

var_dump($final_array);