sql语句不返回任何结果

时间:2018-11-09 14:26:18

标签: php sql json

嗨,我正在sql语句中创建一些虚拟数据,并以jso n格式返回数据。我相信我正在通过odbc连接到mysql db ok。但是,当我在工作台中运行相同的查询时,数据集似乎为空,它返回的数据还可以。

这是将数据返回到进行呼叫的网页的方式

"[{"coords":{"lat":null,"lng":null},"iconImage":null,"content":null},{"coords":{"lat":null,"lng":null},"iconImage":null,"content":null}]  

这是我的代码,我没有错误消息,只是空的json。

require("../PHP/phpsqlajax_dbinfo.php");
$connection=odbc_connect($database, $username, $password);



if (!$connection)echo 'Failed to connect';

//Select Test statement 
$query="select 53.745 as lat,-0.338 as lng,'https://developers.google.com/maps/documentation/javascript/examples/full/images/beachflag.png' as iconImage, '<h1>Tony G</h1>' as content union all
select 53.745 as lat,-0.310 as lng,'https://maps.gstatic.com/mapfiles/ms2/micons/blue.png' as iconImage, '<h1>fred</h1>' as content ";


$result=odbc_exec($connection,$query);

//work through result and create JSON
while ($row = odbc_fetch_row($result)){
    $json[] = [
        'coords' => ['lat' => $row['lat'],'lng' => $row['lng']],
        'iconImage' => $row['iconImage'],
        'content' => $row['content'],
    ]; 
} 
echo json_encode($json);

我对自己做错了事感到困惑。

谢谢

1 个答案:

答案 0 :(得分:1)

尽管从manual尚不清楚odbc_fetch_row中数据的去向,但很明显,此函数的结果(truefalse)不是您期望的。因此,您应该使用另一个函数,该函数返回数组,在这种情况下为odbc_fetch_array

while ($row = odbc_fetch_array($result)){
    $json[] = [
        'coords' => ['lat' => $row['lat'],'lng' => $row['lng']],
        'iconImage' => $row['iconImage'],
        'content' => $row['content'],
    ]; 
}