Birkley Birkley - 26 days ago 8
PHP Question

Return SQL results as simple arrays per column : PHP OCI

I may be using incorrect vernacular in saying "array", but I have a SQL query I'm executing that returns data like the following:

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


I'd like to have it return 4 separate arrays stored as their own variables that look something like:

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


The code I currently have is spitting out an array but it's all wonky...

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

?>


Which gives me a results of...

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" } }


I'm sure it's probably very simple, but i'm a newb with PHP and am still very much learning. Appreciate any help on this one!

Thanks!

Answer

Each column is just an element of the $res array.

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