-
Notifications
You must be signed in to change notification settings - Fork 0
/
sql_to_json.php
45 lines (34 loc) · 1.01 KB
/
sql_to_json.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
<?php
$hostname = $_GET['hostname'];
$username = $_GET['username'];
$password = $_GET['password'];
$querystr = $_GET['querystr'];
$dbname = $_GET['dbname'];
// reference: https://phpdelusions.net/pdo
// for development:
ini_set('display_errors', 1);
// set up PDO
$dsn = "mysql:host=$hostname;dbname=$dbname;charset=utf8";
$opt = [
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
PDO::ATTR_EMULATE_PREPARES => false,
];
$success = false;
try {
$pdo = new PDO($dsn, $username, $password, $opt);
$data = $pdo->query($querystr)->fetchAll();
$success = true;
} catch (PDOException $e) {
$data = $e->getMessage();
}
$data = [$success, $data];
//echo '<pre>';
//var_dump($data);
// headers for not caching the results
header('Cache-Control: no-cache, must-revalidate');
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
// headers to indicate JSON result
header('Content-type: application/json');
// return RESTful json
echo json_encode($data);