简体   繁体   English

Ajax 和 PHP:SELECT 查询数据库

[英]Ajax and PHP : SELECT query database

I have an UI in which the user can add objects (stored as JSON in my database).我有一个用户界面,用户可以在其中添加对象(在我的数据库中存储为 JSON)。 So, when the user is clicking on a button, it calls the ajax wich calls my php to get the desired JSON code.因此,当用户单击按钮时,它调用 ajax 至调用我的 php 以获取所需的 JSON 代码。

The problem is that my ajax returns me "success" but with no JSON code.问题是我的 ajax 返回“成功”但没有 JSON 代码。 I don't know where the problem is located.我不知道问题出在哪里。

Here's my JS/AJAX:这是我的 JS/AJAX:

 var object, objectJson;
    var objectName = $(this).attr("attr-lib");
    var objectId = $(this).attr("attr-id");

    $.ajax({
        url: 'scriptObject.php',
        type: 'POST',
        data: {objectId: objectId},
        dataType: 'json',
        success: function(response) {
            console.log("success");
            console.log(response);
        }
    });

Here's my scriptObject.php :这是我的scriptObject.php

$objectId = $_POST["objectId"];

    $query = "SELECT objectJson FROM object WHERE objectId = ' $objectId '";

    $result = mysql_query($query);

    if($result)
    {
        if(mysql_num_rows($result) > 0)
        {
            $objJSON = mysql_fetch_array($result);
            $res = array("status"=>"success", "objectJson" => $objJSON['objectJson']);
        }
        else
        {
            $res = array("status"=>"error", "message" => "No records found.");
        }
    }
    else
        $res = array("status"=>"error", "message" => "Problem in fetching data.");

    echo json_encode($res);

[EDIT] [编辑]

It answers me:它回答我:

{"status":"error","message":"Problem in fetching data."}

Try following尝试以下

<script type="text/javascript">
    var object, objectJson;
    var objectName = $(this).attr("attr-lib");
    var objectId = $(this).attr("attr-id");

    $.ajax({
        url: 'scriptObject.php',
        type: 'POST',
        dataType : 'json',
        data: { objectId: objectId },
        success: function(response) {
            var json = $.parseJSON(response);
            if(json.status == 'error')
                console.log(json.message);
            else if(json.status == 'success')
                console.log(json.objectJson);
        }
    });
</script>

and in scriptObject.php并在scriptObject.php

<?php 
    // your database connection goes here
    include 'config.php';

    $objectId = $_POST["objectId"];

    $query = "SELECT objectJson FROM object WHERE objectId = ' $objectId '";

    $result = mysql_query($query);

    if($result)
    {
        if(mysql_num_rows($result) > 0)
        {
            $objJSON = mysql_fetch_array($result);
            $res = array("status"=>"success", "objectJson" => $objJSON['objectJson']);
        }
        else
        {
            $res = array("status"=>"error", "message" => "No records found.");
        }
    }
    else
        $res = array("status"=>"error", "message" => "Problem in fetching data.".mysql_error());

    echo json_encode($res);
?> 

scriptObject.php脚本对象.php

if(isset($_POST['objectId']))
{
    $objectId = $_POST["objectId"];
    $query = "SELECT objectJson FROM object WHERE objectId = ' $objectId '";
    $result=mysql_query($query);
        echo json_encode($result);
        die;
}

you are execution the php code inside a function.您正在函数内执行 php 代码。 how does the compiler or the interpreter know to execute a function inside your php file.编译器或解释器如何知道在 php 文件中执行函数。

Add dataType to your ajax optionsdataType添加到您的 ajax 选项

$.ajax({
    url: 'scriptObject.php',
    type: 'POST',
    data: objectId,
    dataType : 'json',
    success: function(response) {
        console.log("success");
        console.log(response);
    }
});

then your php code然后你的php代码

function getObjectJson() {
    $objectId = $_POST["objectId"];
    $query = "SELECT objectJson FROM object WHERE objectId = ' $objectId '";
    $result=mysql_query($query);

    echo json_encode($result);
}

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

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