<?php
/*Servo Recipe*/
/*Recipe Name: An AJAX response in JSON, based on MySQL data*/
/*Recipe Description: This script will return a JSON response, based on a GET request. The response comes from a MySQL database SELECT statement.*/
/*Servo Recipe*/
?>
<?php





/*s3rvo_functiondef_EWAGUJSPXN*/

/*Title: Get a GET Request value*/

/*Object: escapers_get_request_var_easy.json*/

/*Description: Gets a GET request value, with some helpful debugging*/

/*Tint: #2555FF*/

function aer_get_request_value($key) {

	if (isset($_GET[$key])) {

		return $_GET[$key];

	} else {

		echo "There is no GET request entry with the name '".$key."'.<br/>";

		if (count($_GET) == 0) {

			echo "The GET request appears to be entirely empty, are you sure you are calling this script with a GET request?<br/>";

		} else {

			echo "The contents of the GET request is as follows.<br/>";

			var_dump($_GET);

		}

	}

}

/*s3rvo_functiondef_EWAGUJSPXN*/





/*s3rvo_functioncall_BARAYBWFGT*/

/*Title: Get a GET Request value*/

/*Object: escapers_get_request_var_easy.json*/

/*Description: Gets a GET request value, with some helpful debugging*/

/*Tint: #2555FF*/

/*s3rvo_return_value*/$name = /*s3rvo_return_value*/servo_get_request_value(

/*s3rvo_param_$key*/"name"/*s3rvo_param_$key*/

);

/*s3rvo_functioncall_BARAYBWFGT*/





/*s3rvo_functiondef_POGYEJQNGE*/

/*Title: MySQL Connect*/

/*Object: escapers_mysql_connect.json*/

/*Description: Connect to a MySQL database, using the 'imysql' interface*/

/*Tint: #334466*/

function aer_mysql_connect($host,$username,$password,$database) {

	$con = mysqli_connect($host,$username,$password,$database);

	

	return $con;

	

}

                        

                    

/*s3rvo_functiondef_POGYEJQNGE*/







/*s3rvo_functioncall_IEBCXSZWCN*/

/*Title: MySQL Connect*/

/*Object: escapers_mysql_connect.json*/

/*Description: Connect to a MySQL database, using the 'imysql' interface*/

/*Tint: #334466*/

/*s3rvo_return_value*/$con = /*s3rvo_return_value*/servo_mysql_connect(

/*s3rvo_param_$host*/"localhost"/*s3rvo_param_$host*/,

/*s3rvo_param_$username*/"mike"/*s3rvo_param_$username*/,

/*s3rvo_param_$password*/"chunder12"/*s3rvo_param_$password*/,

/*s3rvo_param_$database*/"sakila"/*s3rvo_param_$database*/

);

/*s3rvo_functioncall_IEBCXSZWCN*/



/*s3rvo_functiondef_VVFJVMDSHS*/

/*Title: MySQL - Escape value*/

/*Object: escapers_escape_mysql_value.json*/

/*Description: Escapes (makes safe for use in MySQL) a value*/

/*Tint: #334466*/

function aer_mysql_escape_values($connection,$value) {

		return mysqli_real_escape_string($connection,$value);

}

/*s3rvo_functiondef_VVFJVMDSHS*/





/*s3rvo_functioncall_LEWKMUPYZT*/

/*Title: MySQL - Escape value*/

/*Object: escapers_escape_mysql_value.json*/

/*Description: Escapes (makes safe for use in MySQL) a value*/

/*Tint: #334466*/

/*s3rvo_return_value*/$name= /*s3rvo_return_value*/servo_mysql_escape_values(

/*s3rvo_param_$connection*/$con/*s3rvo_param_$connection*/,

/*s3rvo_param_$value*/$name/*s3rvo_param_$value*/

);

/*s3rvo_functioncall_LEWKMUPYZT*/









/*s3rvo_functiondef_LWPHPZPFBM*/

/*Title: MySQL Query*/

/*Object: escapers_mysql_query_and_return_results.json*/

/*Description: Runs a MySQL query, and returns the results*/

/*Tint: #334466*/

function aer_mysql_query($connection,$query) {

	

	$result = mysqli_query($connection,$query);



	$allrows = array();

	

	while($row = mysqli_fetch_assoc($result)) {

  		array_push($allrows, $row);

	}



	return $allrows;

}

/*s3rvo_functiondef_LWPHPZPFBM*/

/*s3rvo_functioncall_UIBFFQNVUD*/

/*Title: MySQL Query*/

/*Object: escapers_mysql_query_and_return_results.json*/

/*Description: Runs a MySQL query, and returns the results*/

/*Tint: #334466*/

/*s3rvo_return_value*/$result = /*s3rvo_return_value*/servo_mysql_query(

/*s3rvo_param_$connection*/$con/*s3rvo_param_$connection*/,

/*s3rvo_param_$query*/"SELECT * FROM actor WHERE last_name=\"".$name."\" LIMIT 1"/*s3rvo_param_$query*/

);

/*s3rvo_functioncall_UIBFFQNVUD*/











/*s3rvo_functiondef_HPUNSLAKAW*/

/*Title: Output MySQL result set as JSON*/

/*Object: escapers_mysql_results_as_json.json*/

/*Description: Create JSON data from MySQL result set*/

/*Tint: #b65466*/

function aer_mysql_results_as_json($rows) {

	

	$json = array();

	foreach ($rows as $row) {

		array_push($json,$row);

	}    



	return json_encode($json);

}

/*s3rvo_functiondef_HPUNSLAKAW*/









/*s3rvo_functioncall_GAKDXGSTVX*/

/*Title: Output MySQL result set as JSON*/

/*Object: escapers_mysql_results_as_json.json*/

/*Description: Create JSON data from MySQL result set*/

/*Tint: #b65466*/

/*s3rvo_return_value*/echo /*s3rvo_return_value*/servo_mysql_results_as_json(

/*s3rvo_param_$resultset*/$result/*s3rvo_param_$resultset*/

);

/*s3rvo_functioncall_GAKDXGSTVX*/

























































































































































?>