将SQL结果作为每列的简单数组返回:PHP OCI

时间:2016-11-14 21:57:21

标签: php arrays oci

我可能在说“数组”时使用了不正确的白话,但我正在执行的SQL查询返回如下数据:

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

我想让它返回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的新手,我还在学习。感谢对此的任何帮助!

谢谢!

2 个答案:

答案 0 :(得分:1)

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

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

答案 1 :(得分:0)

您必须手动执行标题(第一列)...然后只循环结果:

$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);