PDOStatement to JSON
You can use the inbuilt php function json_encode() http://php.net/manual/en/function.json-encode.php
To encode the results use something like
<?php
$pdo = new PDO("mysql:dbname=database;host=127.0.0.1", "user", "password");
$statement = $pdo->prepare("SELECT * FROM table");
$statement->execute();
$results = $statement->fetchAll(PDO::FETCH_ASSOC);
$json = json_encode($results);
Create JSON from PDO query - PHP
I got the solution to my problem, the thing was that in my DB i have special characters that json_encode
function doesn't understand, to solve it aggregate this line to my code $dbConn->query("SET NAMES 'UTF8'");
:
$dbConn = connect($db);
$dbConn->query("SET NAMES 'UTF8'");
if ($_SERVER['REQUEST_METHOD'] == 'GET'){
$sql = "CALL consulta_administrador()";
$q = $dbConn->query($sql);
$data = $q->fetchAll(PDO::FETCH_ASSOC);
echo json_encode($data, JSON_UNESCAPED_UNICODE);
}
PDO response to json
$dbh->query retuns a PDOStatement object, not the rows. To add the rows to the json, call fetchAll on the object.
$response = $dbh->query("SELECT id, Name FROM Food WHERE userID=1 ORDER BY Name")->fetchAll();
You may also specify the fetch mode, e.g.
fetchAll(PDO::FETCH_ASSOC);
Using PDO to get JSON from tow tables
So finally i got the answer, first the query is correct and perfect, the error in fetching and loop, so this is the perfect solution and much easier:
$rows = array();
while($r = mysqli_fetch_assoc($sth)) {
$r['colors']=explode(',', $r['colors']); //colors like what i named the GROUP_CONCAT
$rows[] = $r;
PDO Output to JSON formatting
To make you code more generic use foreach
instead of for
:
foreach ($list1 as $k => $v) {
$words['output'][] = [
'id' => $k,
'random_combination' => $v . '.' . $list2[$k],
];
}
echo json_encode($words);
pdo and inserting json encoded to json field
You don't put quotes around placeholders. If you quote it, it tries to insert that literal string, instead of replacing it with the value from bindParam()
.
$sql = "INSERT INTO test (config) VALUES(:config);";
PDOStatement to Geojson
I Solved My problem by reading this issue
to have your queried data from Mysql converted to geojson , just try this code :
$geojson = array(
'type' => 'FeatureCollection',
'features' => array()
);
$reponses=$bdd->query('SELECT * FROM `nyc_taxi_data_2014` LIMIT 0,30 ');
while ($data=$reponses->fetch())
{
$marker = array(
'type' => 'Feature',
'features' => array(
'type' => 'Feature',
'properties' => array(
'pickup_time' => "".$data['pickup_datetime']
),
"geometry" => array(
'type' => 'Point',
'coordinates' => array(
$data['pickup_longitude'],
$data['pickup_latitude']
)
)
)
);
array_push($geojson['features'], $marker['features']);
}
echo json_encode($geojson);
Convert PDO recordset to JSON in PHP
The solution is simple.
Considering that the variable $stmt
is your PDO recordset, you can convert it to JSON like this:
json_encode($stmt->fetchAll(PDO::FETCH_ASSOC));
For more info about the functions used in this piece of code:
http://www.php.net/manual/en/function.json-encode.php
http://www.php.net/manual/en/pdostatement.fetchall.php
PHP PDO retrieving json array from database - removing unwanted chars
fetchAll()
returns an array of rows, not the single field you expect:
$statement = $this->db_connection->prepare("SELECT gp FROM gs WHERE c_s= :c_s");
$statement->bindParam(':c_s', $c_s);
$statement->execute();
$rows = $statement->fetchAll();
$data = $rows[0]['gp'];
$result = json_decode($data);
Related Topics
Passing Variables from PHP to JavaScript
Simplexml Get Attributes with a Namespace
How to Get Whole and Decimal Part of a Number
Laravel Eloquent Sort by Relation Table Column
Proper Session Hijacking Prevention in PHP
Simple Way to Read Single Record from MySQL
Laravel Validation: Exists with Additional Column Condition - Custom Validation Rule
Parallel Processing in PHP - How to Do It
Laravel Eloquent Update Just If Changes Have Been Made
Why Use Sprintf Function in PHP
In PHP What Does It Mean by a Function Being Binary-Safe
How to Do Model->Where('Id', Array) Multiple Where Conditions
File Attachment with PHPmailer
Session Timeouts in PHP: Best Practices
MySQL PHP Select Count of Distinct Values from Comma Separated Data (Tags)