做php数据库查询的时候有两个函数
oci_fetch_object和oci_fetch_array(),分别返回object和array类型的返回值,我现在需要一个string类型的,如何转换?
$results = "[";
$row = oci_fetch_array($stmt); <-这里用oci_fetch_object也可以
$results += ??? <-这里如果将返回值$row转换成string类型的,我试过(string)不行
$results = "]";
oci_fetch_object和oci_fetch_array(),分别返回object和array类型的返回值,我现在需要一个string类型的,如何转换?
$results = "[";
$row = oci_fetch_array($stmt); <-这里用oci_fetch_object也可以
$results += ??? <-这里如果将返回值$row转换成string类型的,我试过(string)不行
$results = "]";
其实我的目的就是要把oci查询的返回值变成json串
楼上正解,转化为json是json_encode,json转为array是json_decode
如果想自己拼接也可以啊。数组转化为string,直接用implode好了
$usercount = 0;
$row = oci_fetch_array($stmt);
if($row){
$results += $row;
$usercount++;
}
while (1) {
$row = oci_fetch_array($stmt);
if(!$row) break;
$results += ","+$row;
$usercount++;
}
$results += "]"; 这个是我的代码,得出的$results是0而不是json数组,其中查询是有多条返回值的
$result[] = $row;
}
$result = json_encode($result);
while($row = oci_fetch_array($stmt)) {
$result[] = $row;
}
$result = json_encode($result);
这一段来着,然后返回$result $result[] = $row;提示Cannot use a scalar value as an array in
可以了,刚刚发现在函数开始有个$result的定义来着,删掉就可以了