使用 PHP 从 Oracle 数据库中获取数据并存储到数组中
Fetching data from Oracle database and store into an array using PHP
我正在尝试从数据库中获取数据,date-with-time(timestamp) 和 values(number),并使用 php.
存储到数组中
这是我在数据库中的数据如下=
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 得到以下输出:
[["WD"],["DT"]]
Q1. 为什么我没有得到剩余的数据?我哪里做错了?
但如果我使用
while($row = oci_fetch_row($stid)){
$arr[] = $row;
}
如果我使用 json_encode 那么 =
[["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"],....
如果我使用
while($row = oci_fetch_array($stid,OCI_ASSOC)){
$arr[] = $row;
}
如果我使用 json_encode 那么 =
[{"WD":"25-FEB-15 12.14.00.000000 AM","DT":"15.739993"},{"WD":"25-FEB-15 12.23.00.000000 AM","DT":"13.698263"},........]
我要输出如下=
[["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.如何获取?
请帮忙
因为数据是在关联数组中返回的,所以您会得到返回给您的每一行中每一列的列名和数据。所以这是在 $row
中返回的
"WD" => "25-FEB-15 12.14.00.000000 AM", "DT" => "15.739993"
您需要做的就是从每一行中挑选出数据并像这样忽略 Key :-
$arr = array();
while($row = oci_fetch_array($stid,OCI_ASSOC)){
$arr[] = array($row['WD'] , $row['DT']);
}
echo json_encode($arr);
我正在尝试从数据库中获取数据,date-with-time(timestamp) 和 values(number),并使用 php.
存储到数组中这是我在数据库中的数据如下=
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 得到以下输出: [["WD"],["DT"]]
Q1. 为什么我没有得到剩余的数据?我哪里做错了?
但如果我使用
while($row = oci_fetch_row($stid)){
$arr[] = $row;
}
如果我使用 json_encode 那么 =
[["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"],....
如果我使用
while($row = oci_fetch_array($stid,OCI_ASSOC)){
$arr[] = $row;
}
如果我使用 json_encode 那么 =
[{"WD":"25-FEB-15 12.14.00.000000 AM","DT":"15.739993"},{"WD":"25-FEB-15 12.23.00.000000 AM","DT":"13.698263"},........]
我要输出如下=
[["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.如何获取? 请帮忙
因为数据是在关联数组中返回的,所以您会得到返回给您的每一行中每一列的列名和数据。所以这是在 $row
"WD" => "25-FEB-15 12.14.00.000000 AM", "DT" => "15.739993"
您需要做的就是从每一行中挑选出数据并像这样忽略 Key :-
$arr = array();
while($row = oci_fetch_array($stid,OCI_ASSOC)){
$arr[] = array($row['WD'] , $row['DT']);
}
echo json_encode($arr);