AJAX提供与MySQL不同的结果

时间:2019-01-13 17:51:59

标签: php mysql ajax mysqli

我对AJAX结果有麻烦。我从MySQL的查询中得到了结果,但是AJAX的响应有所不同。我已经将查询等同于MySQL和AJAX。该查询将仅显示 1行或完全不显示。你能告诉我哪里出问题了吗?

这是查询

SELECT a.id_pendaftaran, l.nama_layanan, p2.asal_perusahaan, a.status from
 antrian a JOIN pengunjung p2 ON a.id_pendaftaran = p2.id_pengunjung JOIN 
layanan l ON a.id_layanan = l.id_layanan WHERE a.id_pendaftaran = 'gmFhJd2t0N'

然后是AJAX

   $('.cekbutton').click(function(e){
        var id = $('#id_pendaftaran').val();
            e.preventDefault(); 
            $.ajax({
                url:"ajax.php",
                type:"GET",
                data : "id="+id,
                success : function(result) {
                    console.log(result);
                    var datanew = JSON.parse(result);
                    $('#layanan').val(datanew.layanan);
                    $('#nama_perusahaan').val(datanew.nama);
                    $('#status').val(datanew.status);
                    $('#layanan').prop('disabled', true);
                    $('#nama_perusahaan').prop('disabled', true);
                    $('#status').prop('disabled', true);
                }
            });
        });
<?php

    include "../connection.php";

    $q = mysqli_real_escape_string($koneksi, $_GET['id']);

    $query = mysqli_query($koneksi, "SELECT a.id_pendaftaran, l.nama_layanan,
 p2.asal_perusahaan, a.status from antrian a JOIN pengunjung p2 
ON a.id_pendaftaran = p2.id_pengunjung JOIN layanan l ON a.id_layanan = l.id_layanan 
WHERE a.id_pendaftaran = '".$q."'");
    if (mysqli_num_rows($query) == 1) {
        $row = mysqli_fetch_array($query);

        $nama = $row['asal_perusahaan'];
        $layanan = $row['nama_layanan'];
        $status = $row['status'];

        $myObj = array('hasil' => true, 'nama' => $nama, 'layanan' => $layanan, 'status' => $status);
    }
    else {
        $myObj = array('hasil' => false, 'nama' => "-", 'layanan' => "Belum terdaftar", 'status' => "-");
    }

    $myJSON = json_encode($myObj);
    echo $myJSON;
?>

MySQL的结果

id_pendaftaran | nama_layanan  | asal_perusahaan | status |
gmFhJd2t0N     | Certification | ABC             | Waiting |

这是从浏览器控制台中获得的

{"hasil":false,"nama":"-","layanan":"Belum terdaftar","status":"-"}

如果除此以外更改此公式(mysqli_num_rows($query) == 1),结果将是:

{"hasil":true,"nama":null,"layanan":null,"status":null}

1 个答案:

答案 0 :(得分:0)

也许您的查询返回了多行? if (mysqli_num_rows($query) == 1)