public function select($query) { $link = DatabaseConnection::get(); $result = mysql_query($query, $link); if (!$result) { $this->handleDatabaseError($link); } $rows = array(); while ($row = mysql_fetch_assoc($result)) { array_push($rows, $row); } DatabaseConnection::closeResult($result); DatabaseConnection::closeConnection($link); return $rows; }
<?php class DatabaseConnection { public static function get() { static $db = null; if ($db == null) { $db = new DatabaseConnection(); } return $db; } private $_handle = null; private function __construct() { $this->_handle =& mysql_connect('127.0.0.1', 'root', '12345678'); } public function handle() { return $this->_handle; } } print "Handle = " . DatabaseConnection::get()->handle() . "\n"; print "Handle = " . DatabaseConnection::get()->handle() . "\n"; // $db = new DatabaseConnection(); // vim600:ts=4 st=4 foldmethod=marker foldmarker=<<<,>>> // vim600:syn=php commentstring=//%s
<?php require_once "php/DatabaseConnection.php"; $db = new DatabaseConnection(); $result = $db->get("SELECT * FROM games WHERE id=:id", array(":id" => $_POST['gameId'])); if ($result[0]['guests'] == "") { $guests = ""; } else { $guests = explode(",", $result[0]['guests']); foreach ($guests as $key => $guest) { if ($guest == $_POST['player']) { unset($guests[$key]); } } $guests = array_values($guests); if (count($guests) == 0) { $guestList = ""; } else { $guestList = $guests[0]; for ($i = 1; $i < count($guests); $i++) { $guestList .= "," . $guests[$i]; } } } $db->change("UPDATE games SET guests=:guests WHERE id=:id", array(":id" => $_POST['gameId'], ":guests" => $guestList));
<?php session_start(); require_once "php/DatabaseConnection.php"; $db = new DatabaseConnection(); $result = $db->get("SELECT * FROM games"); echo JSON_encode($result);
public static function escapeStr($value) { $link = DatabaseConnection::get(); $value = mysql_escape_string($value); DatabaseConnection::closeConnection($link); return $value; }
<?php //process Ajax username request $username = htmlspecialchars($_REQUEST['username']); //include db singleton object require_once 'Singleton.php'; $db = DatabaseConnection::get()->handle(); //$db->debug = true; //query to check weather username exist $recordSet =& $db->Execute('select Name from Patterns where Name = "' . $username . '" group by Name'); if ($recordSet->RecordCount() == 1) { echo 1; } else { 0; } $recordSet->Close();
<?php class DatabaseConnection { public static function get() { static $db = null; if ($db == null) { $db = new DatabaseConnection(); } return $db; } private $_handle = null; private function __construct() { $dsn = 'mysql:host=localhost;dbname=laravel'; $this->_handle =& new PDO($dsn, 'root', ''); } public function handle() { return $this->_handle; } } var_dump(DatabaseConnection::get()->handle()); var_dump(DatabaseConnection::get()->handle());
<?php session_start(); require_once "php/DatabaseConnection.php"; $db = new DatabaseConnection(); $results1 = $db->get("SELECT * FROM games WHERE gameName=:gameName", array(":gameName" => $_POST['gameName'])); $results2 = $db->get("SELECT * FROM games WHERE hostName=:hostName", array(":hostName" => $_POST['hostName'])); if (count($results1) > 0) { echo "gameName"; //JSON_encode(array("status"=>"gameName")); } else { if (count($results2) > 0) { echo "hostName"; //JSON_encode(array("status"=>"hostName")); } else { $db->change("INSERT INTO games (gameName,hostName,gameType,guests) VALUES (:gameName,:hostName,:gameType,'')", array(":hostName" => $_POST['hostName'], ":gameName" => $_POST['gameName'], ":gameType" => $_POST['gameType'])); $_SESSION['hostName'] = $_POST['hostName']; $result = $db->get("SELECT * FROM games WHERE hostName=:hostName", array(":hostName" => $_POST['hostName'])); $_SESSION['gameId'] = $result[0]['id']; echo $_SESSION['gameId']; } }
* its default settings. However, you will usually configure * your Slim application now by passing an associative array * of setting names and values into the application constructor. */ $app = new Slim\App(); /** * Step 3: Define the Slim application routes * * Here we define several Slim application routes that respond * to appropriate HTTP request methods. In this example, the second * argument for `Slim::get`, `Slim::post`, `Slim::put`, `Slim::patch`, and `Slim::delete` * is an anonymous function. */ $app->get('/', function ($request, $response, $args) { $response->write("Welcome to Slim!"); $connection = DatabaseConnection::get(); return $response; }); $app->get('/hello[/{name}]', function ($request, $response, $args) { $response->write("Hello, " . $args['name']); return $response; })->setArgument('name', 'World!'); $app->post('/queryServer', function ($request, $response, $args) { $response = $response->withAddedHeader('Content-Type', 'application/json'); $response = $response->withStatus(200); $body = $response->getBody(); $body->write(json_encode(array('salt' => Config_Salts::SaltKey))); return $response; }); /** * Step 4: Run the Slim application
<?php session_start(); require_once "php/DatabaseConnection.php"; $db = new DatabaseConnection(); $result = $db->get("SELECT * FROM gamedata WHERE gameId=:gameId", array(":gameId" => $_SESSION['gameId'])); if (count($result) > 0) { echo $result[count($result) - 1]['gamedata']; }