Skip to content

Instantly share code, notes, and snippets.

@Andrew8xx8
Created March 16, 2011 09:49
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 Andrew8xx8/872246 to your computer and use it in GitHub Desktop.
Save Andrew8xx8/872246 to your computer and use it in GitHub Desktop.
<?php
/**
* Provide a simple innterface to work with MySQL DB
*
* @author Andrew Kulakov
* @copyright 2007 Andrew Kulakov
*
*/
class SimpleMySQL {
/**
* MySQL link resource
* @var MySQL
*/
protected $id;
/**
* Constructor. Used to init MySQL Connection
*
* @param array $config
* Required:
* string $config['host'] host of mysql
* string $config['username'] string username for connect to mysql
* string $config['password'] string password for connect to mysql
* string $config['host'] - db_name
* Not required:
* string $config['charset'] - charset that you want to use for connect
* string $config['collation_connection'] - collation
*/
public function __construct($config) {
$this->id = mysql_connect($config['host'], $config['username'], $config['password'])
or die("Не установлено соединение: ".mysql_error($this->id));
mysql_select_db($config['db_name'], $this->id)
or die('Невозможно выбрать базу данных'.mysql_error($this->id));
if (isset($config['charset'])){
mysql_query ('set names '.$config['charset']);
mysql_query ("set character_set_client='{$config['charset']}'");
mysql_query ("set character_set_results='{$config['charset']}'");
if (isset($config['collation']))
mysql_query ("set collation_connection='{$config['collation']}'");
}
}
/**
* Execute SQL querry.
* Function get format string and variables. Then it plase variables (like printf func) in format string instead of '?' chars
*
* Also function escapes unsafe characters in each variable
*
* @param string $format Contain SQL query width '?' chars wheare you want to plase variables
* @param mixed $asrgs Variables wheare wil be placed instead of '?' signs in formar string
*
* @return MySQL resource on success, or FALSE on error.
*/
public function query($format) {
$args = func_get_args();
$query = "";
$tmpl =& $args[0];
$tmpl = str_replace("%", "%%", $tmpl);
$tmpl = str_replace("?", "%s", $tmpl);
foreach($args as $i=>$v){
if (!$i) continue;
if (is_int($v)) continue;
$args[$i] = "'".mysql_real_escape_string($v)."'";
}
for($i = $c = count($args) - 1; $i < $c + 20; $i++)
$args[$i + 1] = "UNKNOWN_PLACEHOLDER_$i";
$query = call_user_func_array("sprintf", $args);
return mysql_query($query, $this->id);
}
/**
* Makes associative array from MySQL resource
*
* @param resource $resource
*
* @return array
*/
public function makeResult($resource){
for($result = array(); $row = mysql_fetch_array($resource); $result[] = $row);
return $result;
}
/**
* Returns MySQL error
*
* @return string contain MySQL error text
*/
public function getError() {
return mysql_error($this->id);
}
/**
* Magic method that close mysql connection
*/
public function __destruct() {
mysql_close($this->id);
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment