Basically,I want to fetch data from mysql and format it into json like this:
[
[
"title" => "first",
"uploader" => "root"
],
[
"title" => "first",
"uploader" => "root"
],
[
"title" => "first",
"uploader" => "root"
],
[
"title" => "first",
"uploader" => "root"
],
[
"title" => "first",
"uploader" => "root"
],
[
"title" => "first",
"uploader" => "root"
],
[
"title" => "first",
"uploader" => "root"
],
[
"title" => "first",
"uploader" => "root"
],
[
"title" => "first",
"uploader" => "root"
],
[
"title" => "first",
"uploader" => "root"
],
]
I used this code:
<?php
require 'config.php';
$query = $_GET['q'];
if ($query==null) {
exit('No query');
}
$sql = "SELECT title,uploader FROM `uploads_public` WHERE Title =:query ";
if($stmt = $pdo->prepare($sql)){
// Bind variables to the prepared statement as parameters
$stmt->bindParam(":query", $query, PDO::PARAM_STR);
// Attempt to execute the prepared statement
if($stmt->execute()){
echo "Your search $query has the following results(normal json):<br>";
$results = $stmt->fetchAll(PDO::FETCH_ASSOC);
$json = json_encode($results);
echo($json).'<br>';
echo "While it has the following results when replaced<br>";
echo str_replace('}',']',str_replace(':','=>',str_replace('{', '[', "$json")));
} else{
echo "Something went wrong. Please try again later. <br>"; print_r($stmt->errorInfo());
}
// Close statement
unset($stmt);
unset($pdo);
}
else{
echo "No input";
}
But rather than what I wanted(mentioned above),i got this:
[["title"=>"first","uploader"=>"root"],["title"=>"first","uploader"=>"root"],["title"=>"first","uploader"=>"root"],["title"=>"first","uploader"=>"root"],["title"=>"first","uploader"=>"root"],["title"=>"first","uploader"=>"root"],["title"=>"first","uploader"=>"root"],["title"=>"first","uploader"=>"root"],["title"=>"first","uploader"=>"root"]]
It looks ok,but the major problem is that I don't want all the results compressed together ,I want it exactly the way mentioned..with line breaks(new lines) after every ..[
,",
and so on
Thanks