Skip to content

Instantly share code, notes, and snippets.

@ianmac45
Created October 7, 2014 23:10
Show Gist options
  • Save ianmac45/8c45a77ef4071af9fa4c to your computer and use it in GitHub Desktop.
Save ianmac45/8c45a77ef4071af9fa4c to your computer and use it in GitHub Desktop.
retrieving data as json
<?php
header('Content-Type: application/json');
$db = new PDO("mysql:unix_socket=/opt/local/var/run/mariadb/mysqld.sock;dbname=table", "", "");
$result = array('values' => array());
if(isset($_POST["value"]))
{
$val = $_POST["value"];
$q = $db->prepare("select * from table where idx=? limit 1");
$q->bindValue(1, intval(substr($val, 0, 8)), PDO::PARAM_INT);
$q->setFetchMode(PDO::FETCH_ASSOC);
if($q->execute())
if($row = $q->fetch())
$result['values'][] = $row;
}
echo json_encode($result);
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment