1

I'm trying to fetch data from database, date-with-time(timestamp) and values(number) , and storing into an array using php.

here is my data in database as follows=

WD                                  DT

25-FEB-15 12.14.00.000000 AM    15.739993

25-FEB-15 12.23.00.000000 AM    13.698263   

25-FEB-15 12.43.00.000000 AM    13.214383

fetch.php

<?php
include("md.php");
$sql = "SELECT * from datatable";
$result =oci_parse($conn, $sql);
$r=oci_execute($result);
$arr = array();
$row=oci_num_rows($stid);
$arr[0]=array('wd','dt');

for($i=1; $i<($row+1); $i++)
{
    $arr[$i]= array(substr(oci_result($result, $i-1, "wd"),0,18),(float)oci_result($result,$i-1,"dt"));
    //$arr[$i]= array(substr(oci_result($result, $i-1, "wd"),0,18),(int)oci_result($result,$i-1,"dt"));

}
echo json_encode($arr);
//print_r($arr);
?>

$arr getting following output: [["WD"],["DT"]]

Q1. Why am i not getting rest of data? where am i doing wrong?

but if i use

while($row = oci_fetch_row($stid)){

    $arr[] = $row;
}

if i use json_encode then =

[["25-FEB-15 12.14.00.000000 AM","15.739993"],["25-FEB-15 12.23.00.000000 AM","13.698263"],["25-FEB-15 12.43.00.000000 AM","13.214383"],....

if i use

while($row = oci_fetch_array($stid,OCI_ASSOC)){

  $arr[] = $row;
}

if i use json_encode then =

[{"WD":"25-FEB-15 12.14.00.000000 AM","DT":"15.739993"},{"WD":"25-FEB-15 12.23.00.000000 AM","DT":"13.698263"},........]

I want the output as follows=

[["25-FEB-15 12.14.00 AM",15.739993],["25-FEB-15 12.23.00 AM",13.698263],["25-FEB-15 12.43.00 AM",13.214383],....]

Q2. How can i get it? please help

1 Answer 1

1

Because the data is being returned in an associative array you get the column name and the data for each column in each row returned to you. So this is being returned in $row

"WD" => "25-FEB-15 12.14.00.000000 AM", "DT" => "15.739993"

All you need to do is pick out the data from each row and ignore the Key like so :-

$arr = array();

while($row = oci_fetch_array($stid,OCI_ASSOC)){

  $arr[] = array($row['WD'] , $row['DT']);
}

echo json_encode($arr);
Sign up to request clarification or add additional context in comments.

1 Comment

thanks for your response. now i figured it out how to get 'DT' value as my required output. thanks a lot

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.