0

I retrieve JSON from php:

<?php
 require("includes/connection.php");

//$queryString = "home";
$returnArray = array(); 

if (!$server) 
{ 
    die('Connect Error (' . mysqli_connect_errno() . ')' . mysqli_connect_error() );  
} 


$query =    "SELECT * 
            FROM stuff
            WHERE name LIKE '%" . $queryString . "%' 
            ORDER BY name LIMIT 1";

if($result = $server->query($query))
{
    while ($row = $result->fetch_assoc())
    {
        array_push($returnArray, $row);
        
    }


}           
   
echo json_encode($returnArray);

?>

And javascript:

var home = "home";
  $.getJSON("db/getJSON.php", {queryString: ""+home+""},
    
function(data){
            
    jsn = JSON.stringify(data);
    $("#outputtester").html(jsn);
        consoleOut("JSON: "+jsn);

   });

It outputs quite a nice string:

[{"UID":"1","IDS":"1,2,3","name":"home","type":"thing","cat_id":"home"}]

But I think the two [] are not supposed to be there, because it is not an array of arrays?

I tried putting echo "Things: ". before the json_encode, so I could identify the array of arrays. But it wont work

I tried to access 'data' without stringifying, as an object, it should work with something like:

data.UID

or

data.name

but I get an object Object output each time, also when I stringify them..

Any idea what I'm doing wrong?

Can't find a whole bunch of info on working with JSON arrays from, all the stuff I find manually make the JSON in javascript code..

2
  • 1
    $returnArray is an array of arrays, so the result seems to be fine. $returnArray is an array. Each $row is an array. You are adding $row to $returnArray -> array of arrays. Of course if your query is only returning one table row you end up with an array containing only one array. Maybe you want to assign $row to $returnArray instead. Commented Feb 22, 2012 at 17:39
  • 1
    1) your code is wide open to SQL injection attacks, 2) That JSON output is correct, you are json encoding an array of rows of data where the rows are 'objects'. There is no issue here (except your query code). Commented Feb 22, 2012 at 17:42

2 Answers 2

1

Remove the while loop

<?php
 require("includes/connection.php");

$row = array(); 

if (!$server) 
{ 
    die('Connect Error (' . mysqli_connect_errno() . ')' . mysqli_connect_error() );  
} 


$query =    "SELECT * 
            FROM stuff
            WHERE name LIKE '%" . $queryString . "%' 
            ORDER BY name LIMIT 1";
    
    if ($result = $server->query($query)) {
        $row = $result->fetch_assoc(); // Fetch a single row
        
    }
    
    echo json_encode($row);

You can also modify your query to prevent sql injections

$query = "SELECT * 
          FROM stuff
          WHERE name LIKE ? 
          ORDER BY name LIMIT 1";

if ($stmt = $server->prepare($query)) {
    // Bind the parameter to the query
    $searchParam = "%" . $queryString . "%"; // Add wildcards for the LIKE clause
    $stmt->bind_param("s", $searchParam); // 's' denotes a string parameter

    // Execute the prepared statement
    $stmt->execute();

    // Get the result
    $result = $stmt->get_result();
    $row = $result->fetch_assoc();
       // Close the statement
    $stmt->close();
}
Sign up to request clarification or add additional context in comments.

Comments

1

Change :

echo json_encode($returnArray);

To:

if($returnArray) {
    echo json_encode($returnArray[0]);
} else{
    // here echo whatever you want to signify there were no results.
}

1 Comment

That's if he only wants to return the first result, which makes a while loop stupid.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.