Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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. For some reason it never arrives (check with Firebug). But if I manually create a 2D array, json_encode it, it works fine. I am completely stumped why my array never makes it from SQL but it does if I just hand type it. 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
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.

share|improve this question
    
have to tried to turn on the error reporting? and check? and instead of console.log('results'), you actually put the response instead console.log(results) (without the quotes)? –  Ghost Sep 17 at 6:29
    
Do you have any users with the exact username 's'? You're using "=" in your sql where it looks like you might mean "like" –  Simon Brahan Sep 17 at 6:46
    
Here is an exact copy of the resulting dataset json_encode'ed from SQL: [{"id":7,"first_name":"Tom","last_name":"Cruise"},{"id":8,"first_name":"John","l‌​ast_name":"Cusak"}] –  seang Sep 17 at 14:42

2 Answers 2

up vote 1 down vote accepted

I figured out my problem. The line in the Ajax where I set the data:

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.

share|improve this answer
    
Remember: every jQuery DOM selector function $(something) returns an array; $('#staff_list').first().val() is what you probably wanted. –  Dan Crews Sep 17 at 15:19
    
That is good to know. Thank you. –  seang Sep 20 at 2:53

Can you please post your result set here which is return by the satement.
$result->fetch_assoc().

Please check your php version. Because the adding array element by using shorten method as you have used in your code "$json[] = $row;" is available in 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); instead of $json[] = $row;

share|improve this answer
    
Here is the print_r'ed output of that array: Array ( [0] => Array ( [id] => 7 [first_name] => Tom [last_name] => Cruise ) [1] => Array ( [id] => 8 [first_name] => John [last_name] => Cusak ) ) –  seang Sep 17 at 14:44

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.