将ajax转换为php并从中获取JSON

将ajax转换为php并从中获取JSON,php,jquery,json,Php,Jquery,Json,我有一个php文件,我用ajax连接到它,回调值是JSON 当我从php获取数据时,它不会显示,当我看到对象时,会显示警报数据 我的问题在哪里 PHP: 我如何从这个JSON中获取所有数据并向我展示它的正确性?我建议使用以下任一方法: b = jQuery.parseJSON(data) 看更多 或 这里不是eval,而是一个完整的单行获取和多行获取的工作示例,它不使用mysql语法,也不使用预先准备好的语句来防止sql注入 是的,不要使用mysql特定的语法,就像我在这里提到的: 如果未设置

我有一个php文件,我用ajax连接到它,回调值是JSON 当我从php获取数据时,它不会显示,当我看到对象时,会显示警报数据

我的问题在哪里

PHP:


我如何从这个JSON中获取所有数据并向我展示它的正确性?

我建议使用以下任一方法:

b = jQuery.parseJSON(data)
看更多 或


这里不是eval,而是一个完整的单行获取和多行获取的工作示例,它不使用mysql语法,也不使用预先准备好的语句来防止sql注入

是的,不要使用mysql特定的语法,就像我在这里提到的:

如果未设置$select的值,则三元运算符会使其变为false

确保您可以在此处访问您的数据库:

$db = $GLOBALS['db']; // An example of a PDO database connection
现在,检查$select请求是否为true,然后执行一些数据库请求,并用JSON返回它们:

if($select)
{
    // Fetch data from the database.
    // Return the data with a JSON array (see below).
}
else
{
    $json[] = array
    (
        'message' => 'Not Requested'
    );
}
echo json_encode($json);
flush();
if($select)
{
    // Assume that the previously defined query exists.
    $results = getSingleRow($db, $query);
    if($results !== false)
    {
         $json[] = array
         (
             'result1' => $results['result1'],
             'result2' => $results['result2'],
             'message' => 'success'
         );
    }
    else // Nothing found in database
    { 
        $json[] = array
        (
             'message' => 'nothing found'
        );
    }
}
// ...
当然,从数据库获取数据的方式是可选的,您可以使用JSON从数据库获取一行数据,也可以使用JSON返回多行数据

让我举一个示例,说明如何使用json返回多行数据,并在javascript中对这些数据进行迭代:

function selectMultipleRows($db, $query)
{
    $array = array();
    $stmt = $db->prepare($query);
    $stmt->execute();
    if($result = $stmt->fetchAll(PDO::FETCH_ASSOC))
    {
        foreach($result as $res)
        {
            foreach($res as $key=>$val)
            {
                $temp[$key] = utf8_encode($val);
            }
            array_push($array, $temp);
        }
        return $array;
    }
    return false;
}
然后你可以这样做:

if($select)
{
    $array = array();
    $i = 0;

    $query = 'SELECT e.result1, e.result2 FROM exampleTable e ORDER BY e.id ASC;';
    foreach(selectMultipleRows($db, $query) as $row)
    {
        $array[$i]["result1"] = $row['result1'];
        $array[$i]["result2"] = $row['result2'];
        $i++;
    }

    if(!(empty($array))) // If something was fetched
    {
        while(list($key, $value) = each($array))
        {
             $json[] = array
             (
                 'result1' => $value["result1"],
                 'result2' => $value["result2"],
                 'message' => 'success'
             );
       }
    }
    else // Nothing found in database
    { 
        $json[] = array
        (
             'message' => 'nothing found'
        );
    }
}
// ...
或者,如果你想接吻,保持简单愚蠢:

Init一个基本函数,从数据库中选择一些值并返回一行:

function getSingleRow($db, $query)
{
    $stmt = $db->prepare($query);
    $stmt->execute();
    // $stmt->execute(array(":id"=>$someValue)); another approach to execute.
    $result = $stmt->fetch(PDO::FETCH_ASSOC);
    if($result)
    {
        $array = (
            'result1' => $result['result1'], 
            'result2' => $result['result2']
        );
        // An array is not needed for a single value.
        return $array;
    }
    return false;
}
然后获取行或单个值并用JSON返回:

if($select)
{
    // Fetch data from the database.
    // Return the data with a JSON array (see below).
}
else
{
    $json[] = array
    (
        'message' => 'Not Requested'
    );
}
echo json_encode($json);
flush();
if($select)
{
    // Assume that the previously defined query exists.
    $results = getSingleRow($db, $query);
    if($results !== false)
    {
         $json[] = array
         (
             'result1' => $results['result1'],
             'result2' => $results['result2'],
             'message' => 'success'
         );
    }
    else // Nothing found in database
    { 
        $json[] = array
        (
             'message' => 'nothing found'
        );
    }
}
// ...
如果您想获得$tab的值,那么必须执行类似$tab.val或$tab.text的操作


我希望这会有所帮助。

使用console.dir而不是console.log。$'tab'到底是什么?为什么不在ajax调用中设置dataType:json,让jQuery自己执行parseJSON步骤?那么为什么jQuery提出这两个备选方案呢?如果您看到jQuery还提到$.getJSON比$.ajax短{dataType:json,对不起,第一行是盲目的。getJSON在这里看起来是最简单的。
function getSingleRow($db, $query)
{
    $stmt = $db->prepare($query);
    $stmt->execute();
    // $stmt->execute(array(":id"=>$someValue)); another approach to execute.
    $result = $stmt->fetch(PDO::FETCH_ASSOC);
    if($result)
    {
        $array = (
            'result1' => $result['result1'], 
            'result2' => $result['result2']
        );
        // An array is not needed for a single value.
        return $array;
    }
    return false;
}
if($select)
{
    // Assume that the previously defined query exists.
    $results = getSingleRow($db, $query);
    if($results !== false)
    {
         $json[] = array
         (
             'result1' => $results['result1'],
             'result2' => $results['result2'],
             'message' => 'success'
         );
    }
    else // Nothing found in database
    { 
        $json[] = array
        (
             'message' => 'nothing found'
        );
    }
}
// ...