Here is a php to mysql communication gateway packaged as a web-service.
Gateway.php
Config.php
CMySqlManager.class.php
Gateway.php
<?php
require_once('Config.php');
require_once('engine/CMySqlManager.class.php');
$VoCMySqlManager = new CMySqlManager();
$VoCMySqlManager->Connect(DB_HOST, DB_USERNAME, DB_PASSWORD, DB_SCHEMA);
$_POST['message'] = (!isset($_POST['message']))? null : $_POST['message'];
$_GET['message'] = (!isset($_GET['message']))? null : $_GET['message'];
$C2S_message = ($_POST['message'] == null)? $_GET['message'] : $_POST['message'];
$S2S_information = unserialize(stripslashes(urldecode($C2S_message)));
if(isset($S2S_information->COMMAND) && isset($S2S_information->QUERY))
{
$S2C_output = array();
$S2C_output = $VoCMySqlManager->Process($S2S_information->QUERY, $S2S_information->COMMAND);
if($S2S_information->COMMAND)
array_pop($S2C_output);
echo urlencode(serialize($S2C_output));
//echo urlencode(serialize($S2S_information->QUERY));
}
?>
Config.php
<?php
$parentDir = dirname(__FILE__);
define('APPROOT',$parentDir.'/');
$serverName = $_SERVER['SERVER_NAME'];
define('WEBROOT','http://'.$serverName.'/test/');
define('DB_HOST', $serverName);
define('DB_USERNAME', 'root');
define('DB_PASSWORD', '');
define('DB_SCHEMA', 'test_db');
?>
CMySqlManager.class.php
<?php
/**
* @Class: CMySqlManager
* @Author: Abhishek Kumar
**/
class CMySqlManager
{
public $mysql_host;
public $mysql_user;
public $mysql_password;
public $mysql_database;
public function Connect($iHost, $iUser, $iPassword, $iDatabase)
{
$this->mysql_host = $iHost;
$this->mysql_user = $iUser;
$this->mysql_password = $iPassword;
$this->mysql_database = $iDatabase;
}
public function Process($iQuery, $iReturn)
{
if($Connection = mysql_connect($this->mysql_host, $this->mysql_user, $this->mysql_password))
if($Database = mysql_select_db($this->mysql_database))
if($Result = mysql_query($iQuery))
{
if($iReturn)
{
while ($Data[] = mysql_fetch_array($Result, MYSQL_ASSOC));
mysql_free_result($Result);
}
mysql_close($Connection);
return $Data;
}
else
return 'Error# '.mysql_error();
else
return 'Error# '.mysql_error();
else
return 'Error# '.mysql_error();
}
// deprecated
public function Execute($iQuery)
{
if($Connection = mysql_connect($this->mysql_host, $this->mysql_user, $this->mysql_password))
if($Database = mysql_select_db($this->mysql_database))
if($Result = mysql_query($iQuery))
mysql_close($Connection);
else
return 'Error# '.mysql_error();
else
return 'Error# '.mysql_error();
else
return 'Error# '.mysql_error();
}
// deprecated
public function Query($iQuery)
{
if($Connection = mysql_connect($this->mysql_host, $this->mysql_user, $this->mysql_password))
if($Database = mysql_select_db($this->mysql_database))
if($Result = mysql_query($iQuery))
{
while ($Data[] = mysql_fetch_array($Result, MYSQL_ASSOC));
mysql_free_result($Result);
mysql_close($Connection);
return $Data;
}
else
return 'Error# '.mysql_error();
else
return 'Error# '.mysql_error();
else
return 'Error# '.mysql_error();
}
}
/*
// Example
require_once('CMySqlManager.php');
$VoCMySqlManager = new CMySqlManager();
$VoCMySqlManager->Connect('localhost','root','password','test');
//$query = "INSERT INTO names VALUES('','Sonali','','Sharma','sonali@gmail.com')";
//$output = $VoCMySqlManager->Execute($query);
$query = 'SELECT * FROM names';
$output = $VoCMySqlManager->Query($query);
//$query = 'SELECT * FROM names';
//$output = $VoCMySqlManager->Process($query, true);
print_r($output);
*/
?>
Comments
Post a Comment