简体   繁体   English

PHP和JQuery-PHP json_encoded 2D数组未到达JQuery Ajax中

[英]PHP and JQuery - PHP json_encoded 2D array doesn't arrive in JQuery Ajax

I am trying to create a demo to get data from a PHP script that runs a SQL query, takes the associated array and json_encodes's it and returns it to the JQuery ajax caller in the calling php file. 我正在尝试创建一个演示,以从运行SQL查询的PHP脚本获取数据,获取关联的数组和json_encodes的数组,并将其返回给调用php文件中的JQuery ajax调用者。 For some reason it never arrives (check with Firebug). 由于某种原因,它永远不会到达(请与Firebug一起检查)。 But if I manually create a 2D array, json_encode it, it works fine. 但是,如果我手动创建一个2D数组json_encode,它可以正常工作。 I am completely stumped why my array never makes it from SQL but it does if I just hand type it. 我完全感到困惑,为什么我的数组从不从SQL生成它,但是如果我只是手动键入它就可以。 I have diffed the resulting strings and they are exactly the same. 我已经比较了结果字符串,它们是完全一样的。

Code: 码:

...snip...
$.ajax({
        type: "GET",
        url: "getclients.php",
        data: { username: $('#staff_list').val() },
        //contentType: "application/json",
        dataType: 'json',
        success: function(results) {
            console.log("results");
        },
        fail: function() {
            console.log("fail!!");
        },
        error: function(r, e, m) {
            console.log("error");
            //console.dir(r);
            console.log(e + ', ' + m);
        }
    })
    .done(function(data) {
        console.log("done");
        //console.log(data);
    });
    console.log("done with change detection...");    
...snip...

PHP File: PHP文件:

<?php
header('Content-Type: application/json');
error_reporting(0); // prevents a notice from breaking ajax
//session_start();
$username = $_GET['username'];
$json = array();
$test = array(
    array("id"=>4,"first_name"=>"Miles","last_name"=>"O'Brian"),
    array("id"=>5,"first_name"=>"Jean Luc","last_name"=>"Picard"),
    array("id"=>6,"first_name"=>"Reginald","last_name"=>"Barclay")
          );

$mysqli = new mysqli('mydomain', 'myuser', 'mypassword', 'mydb');
if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
}
if ($stmt = $mysqli->prepare("SELECT `id`, `first_name`, `last_name` FROM client WHERE provider_username = ?")) {
    $stmt->bind_param('s', $username);
    $stmt->execute();
    $result = $stmt->get_result();


    while ($row = $result->fetch_assoc()) {
        $json[] = $row;
    }

    $result->close();
}

$mysqli->close();
echo json_encode($json);
echo json_encode($test);// this works
?>    

Any help with this would be appreciated. 任何帮助,将不胜感激。 I am new to PHP but not to programming so I tried everything I can think of (including the manual and Google) and am just completely stumped. 我是PHP新手,但不是编程新手,所以我尝试了所有我能想到的东西(包括手册和Google),但完全陷入了困境。

I figured out my problem. 我发现了我的问题。 The line in the Ajax where I set the data: 我在Ajax中设置数据的行:

data: { username: $('#staff_list').val() }

is causing the problem. 造成了问题。 For some reason it is getting an array, making the line 由于某种原因,它获取了一个数组,使行

data: { username: $('#staff_list').val()[0] }

fixes the problem. 解决问题。

Can you please post your result set here which is return by the satement. 您能在这里发布结果集吗?
$result->fetch_assoc(). $ result-> FETCH_ASSOC()。

Please check your php version. 请检查您的php版本。 Because the adding array element by using shorten method as you have used in your code "$json[] = $row;" 因为使用代码中使用的缩短方法添加数组元素“ $ json [] = $ row;” is available in PHP 5.4. 在PHP 5.4中可用。 Look document for more information. 查看文档以获取更多信息。

Second option if it is not available in your PHP version then you can achieve it as: array_push($json,$row); 第二种选择,如果它在您的PHP版本中不可用,则可以通过以下方式实现:array_push($ json,$ row); instead of $json[] = $row; 而不是$ json [] = $ row;

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

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