Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
<?php
header('Content-Type: application/json');
//--------------------------------------------------------------------------
// Example php script for fetching data from mysql database
//--------------------------------------------------------------------------
$host = "localhost";
$user = "";
$pass = "";
$databaseName = "";
$tableName = "suggestions";
//--------------------------------------------------------------------------
// 1) Connect to mysql database
//--------------------------------------------------------------------------
$con = mysql_connect($host,$user,$pass);
$dbs = mysql_select_db($databaseName, $con);
$name=$_REQUEST['name'];
//--------------------------------------------------------------------------
// 2) Query database for data
//--------------------------------------------------------------------------
$result = mysql_query("SELECT * FROM $tableName where name like '$name%%'"); //query
//$array = mysql_fetch_row($result); //fetch result
$rows = array();
while($r = mysql_fetch_assoc($result)) {
$rows['results'][] = $r;
}
//--------------------------------------------------------------------------
// 3) echo result as json
//--------------------------------------------------------------------------
echo json_encode($rows);
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment