Skip to content

Instantly share code, notes, and snippets.

@HZooly
Created January 2, 2018 14:34
Show Gist options
  • Save HZooly/0141095215c413c163721dcd611a3c60 to your computer and use it in GitHub Desktop.
Save HZooly/0141095215c413c163721dcd611a3c60 to your computer and use it in GitHub Desktop.
PDO JSON
<?php
$db_name = 'example';
$hostname = '127.0.0.1';
$username = 'root';
$password = 'root';
try{
$dbh = new PDO("mysql:host=$hostname;dbname=$db_name", $username, $password);
$sql = 'INSERT INTO user (name, email, age) VALUES (?,?,?)';
$stmt = $dbh->prepare($sql);
$name = $_POST['name'];
$email = $_POST['email'];
$age = $_POST['age'];
$stmt->bindParam(1, $name);
$stmt->bindParam(2, $email);
$stmt->bindParam(3, $age);
$stmt->execute();
$result = $stmt->fetchAll(PDO::FETCH_ASSOC);
$json = json_encode($result);
echo $json;
} catch (Exception $e){
die($e->getMessage());
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment