简体   繁体   English

sql语句不返回任何结果

[英]sql statement not returning any results

Hi I am creating some dummy data in a sql statement and returning the data in a jso n format.嗨,我正在 sql 语句中创建一些虚拟数据并以 json n 格式返回数据。 I believe I am connecting to the mysql db ok through odbc.我相信我正在通过 odbc 连接到 mysql db。 However the dataset appears to be empty when I run the same query in workbench it returns data ok.但是,当我在工作台中运行相同的查询时,数据集似乎为空,它返回数据正常。

This is how the data is returned to the webpage making the call这是数据返回到进行调用的网页的方式

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

here is my code I have no error messages just empty json.这是我的代码我没有错误消息只是空的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);

I am a little puzzled as to what I am doing wrong.我对我做错了什么感到有点困惑。

thanks谢谢

Though it's unclear from manual where does data go to in odbc_fetch_row , it's clear that result ( true or false ) of this function is not what you expect.尽管手册中不清楚odbc_fetch_row中的数据在odbc_fetch_row ,但很明显该函数的结果( truefalse )不是您所期望的。 So, you should use another function, which returns array, in this case it is odbc_fetch_array :因此,您应该使用另一个返回数组的函数,在本例中它是odbc_fetch_array

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM