dougang6178 2016-05-12 07:46
浏览 78

JSON:查询连接表不起作用(Cordova Mobile)

I have 2 query the result of which I will use in JSON . The first query is

$query = "SELECT * 
            FROM info_lokasi 
           WHERE id_lokasi=".$id."";

and the second query

$query = "SELECT t1.id_lokasi,t1.jalan,t1.lat,t1.lng,t1.nama,t2.kapasitas,t2.kegiatan,t2.parkir,t2.pengurus
            FROM info_lokasi t1 
      INNER JOIN detail t2 ON t1.id_lokasi=t2.id_lokasi
           WHERE t1.id_lokasi 
             AND t2.id_lokasi=".$id."";

when I use the first query , the result I get is

[
{
"id_lokasi": "3",
"nama": "masjid",
"jalan": "surabaya",
"lat": "-7.292958471571183",
"lng": "112.71161556243896",
"username": "ope",
"jenis": "masijd"
}
]

but, when I use a second query , I did not get the result. WHY?

this is my code:

PHP

<?php 
    include "db.php";
    header("Content-type: text/javascript");

    if (isset($_POST['detail'])) {
    $id = $_POST['id'];

    $query = "SELECT t1.id_lokasi,t1.jalan,t1.lat,t1.lng,t1.nama,t2.kapasitas,t2.kegiatan,t2.parkir,t2.pengurus FROM info_lokasi t1 INNER JOIN detail t2 ON t1.id_lokasi=t2.id_lokasi WHERE t1.id_lokasi AND t2.id_lokasi=".$id."";

    //$query = "SELECT * FROM info_lokasi WHERE id_lokasi=".$id."";

    $data = $con->query($query);

    $results = array();
    while ($r = mysqli_fetch_array($data,MYSQL_ASSOC)) {
        # code...
        $results[] = $r;
    }
    echo json_encode($results);
    }
    mysqli_close($con);

?>

Javascript

function detail(idlokasi){
    var id = idlokasi;
    var datastring = "id="+id+"&detail=";
            $.ajax({
                type: "POST",
                url: "http://localhost:8080/maps-api/detail.php",
                data: datastring,
                crossDomain: true,
                cache: false,
                datatype: "JSON",
                success: function(data){
                    alert("DATA: "+data);                
                } 
            });
}

Thanks

  • 写回答

1条回答 默认 最新

  • duanchu2607 2016-05-12 08:02
    关注

    The reason why you don't get any output can be:

    1. Query really returns no data (i.e.: no rows match WHERE conditions)
    2. There's an error on your SQL

    As you expected it to return data I'll assume that is case 2.

    Your query is:

          SELECT t1.id_lokasi,t1.jalan,t1.lat,t1.lng,t1.nama,t2.kapasitas,t2.kegiatan,t2.parkir,t2.pengurus
            FROM info_lokasi t1 
      INNER JOIN detail t2 ON t1.id_lokasi=t2.id_lokasi
           WHERE t1.id_lokasi 
             AND t2.id_lokasi=$id
    

    To detect the error, you should use this PHP code:

    $query = "SELECT t1.id_lokasi,t1.jalan,t1.lat,t1.lng,t1.nama,t2.kapasitas,t2.kegiatan,t2.parkir,t2.pengurus
            FROM info_lokasi t1 
      INNER JOIN detail t2 ON t1.id_lokasi=t2.id_lokasi
           WHERE t1.id_lokasi 
             AND t2.id_lokasi=$id";
    $data = $con->query($query);
    if ($con->error)
    {
        echo "ERROR: {$con->error}";
        throw new Exception($con->error);
    }
    

    Note: don't use $_POST['id'] as your parameter as it can lead to SQL injection problems. You should better use query parameters.

    评论

报告相同问题?

悬赏问题

  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c