×

Welcome to TagMyCode

Please login or create account to add a snippet.
0
0
 
0
Language: PHP
Posted by: user7784c
Added: May 22, 2017 6:48 PM
Views: 7
Use the inbuilt php function json_encode()
  1. // To encode the statement use something like this:
  2. <?php
  3.         $pdo=new PDO("mysql:dbname=database;host=127.0.0.1","user","password");
  4.         $statement=$pdo->prepare("SELECT * FROM table");
  5.         $json=json_encode($statement);
  6. ?>
  7.  
  8. // To encode just the results use something like
  9.  
  10. <?php
  11. $pdo=new PDO("mysql:dbname=database;host=127.0.0.1","user","password");
  12. $statement=$pdo->prepare("SELECT * FROM table");
  13. $statement->execute();
  14. $results=$statement->fetchAll(PDO::FETCH_ASSOC);
  15. $json=json_encode($results);
  16. ?>