Skip to content

Instantly share code, notes, and snippets.

@manishkpr
Created December 30, 2016 07:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save manishkpr/b450c602d11a3ceb3139464a8ed35377 to your computer and use it in GitHub Desktop.
Save manishkpr/b450c602d11a3ceb3139464a8ed35377 to your computer and use it in GitHub Desktop.
<?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