Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /*
- Usage Example:
- $db = new Connection('MySite');
- $result = $db->query("select * from Table where someField='%s' and someOtherField='%s'", array($value1, $value2));
- From here on you just handle $result like a normal response from a mysql_query().
- */
- if(basename(__FILE__) == basename($_SERVER['PHP_SELF'])){header('Location: index.php');} //makes this file include-only by checking if the requested file is the same as this file
- class Connection
- {
- protected $connection;
- #default values
- private $server="localhost", $username="root", $password="", $database="pentest";
- function __construct()
- {
- $a = func_get_args();
- $i = func_num_args();
- if (method_exists($this,$f='__construct'.$i)) {
- call_user_func_array(array($this,$f),$a);
- }
- }
- public function __construct1($database)
- {
- $this->database = $database;
- $this->connect();
- }
- public function __construct4($server, $username, $password, $database)
- {
- $this->server = $server;
- $this->username = $username;
- $this->password = $password;
- $this->database = $database;
- $this->connect();
- }
- private function connect()
- {
- $this->connection = mysql_connect($this->server, $this->username, $this->password);
- mysql_select_db($this->database, $this->connection);
- }
- public function sanitize($formatString, array $args)
- {
- $len = count($args);
- for($arg=0; $arg < $len; $arg++)
- {
- $args[$arg] = mysql_real_escape_string($args[$arg]);
- }
- return vsprintf($formatString, $args);
- }
- public function query($formatString, array $args)
- {
- $q = $this->sanitize($formatString, $args);
- $result = mysql_query($q);
- return $result;
- }
- public function close()
- {
- mysql_close($this->connection);
- }
- }
- ?>
Add Comment
Please, Sign In to add comment