Skip to content

Instantly share code, notes, and snippets.

@siqin
Created March 3, 2012 10:16
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 siqin/1965382 to your computer and use it in GitHub Desktop.
Save siqin/1965382 to your computer and use it in GitHub Desktop.
Simple PHP2MySQL Class
<?php
/*
* FYSMysql.php Created on 2012-3-3
* By Jason Lee
*
* 后面可以考虑抽象成模型,每个表对应一个Model子类,自动探测表结构,把错误检查放在Model中
*
*/
class FYSMysql {
var $connect;
function __construct( $config = array() ) {
$defaultConfig = array(
'host' => 'localhost',
'user' => 'root',
'password' => '',
'db' => 'fys'
);
$dbConfig = array_merge($defaultConfig, $config);
$this->connect = mysql_connect(
$dbConfig['host'],
$dbConfig['user'],
$dbConfig['password'],
TRUE /*Always open a new link*/
) or die('Error : Failed to connect mysql.');
mysql_select_db(
$dbConfig['db'],
$this->connect
) or die('Error : Failed to select database ' . $dbConfig['db'] . '.');
}
function __destruct() {
mysql_close($this->connect);
}
private function execute($query = "") {
return mysql_query($query, $this->connect);
}
private function mergeArrayElementsForMysql($array) {
/* Function 'implode' only creates one string, like 'join' in Python */
return implode(',', $array);
}
/* CURD Below */
function insert($table=null, $array=array()) {
if ( null == $table || empty($array) || !is_array($array) ) {
return FALSE;
}
$fields = array();
$values = array();
foreach ($array as $key => $value) {
$fields[] = $key; /* Better than 'array_push' only one element */
$values[] = "'" . mysql_real_escape_string($value, $this->connect) . "'";
}
$filedStr = $this->mergeArrayElementsForMysql($fields);
$valueStr = $this->mergeArrayElementsForMysql($values);
$query = "INSERT INTO $table ($filedStr) VALUES ($valueStr)";
if ($this->execute($query)) {
return mysql_insert_id($this->connect);
}
return FALSE;
}
function delete($table=null, $condition='1=0') {
if (null != $table) {
$query = "DELETE FROM $table WHERE $condition";
return $this->execute($query);
}
}
function update($table=null, $array=array(), $condition='1=1') {
if ( null == $table || empty($array) || !is_array($array) ) {
return FALSE;
}
$array2set = array();
foreach ($array as $key => $value) {
$array2set[] = $key . ' = ' .
"'" . mysql_real_escape_string($value, $this->connect) . "'";
}
$setStr = $this->mergeArrayElementsForMysql($array2set);
$query = "UPDATE $table SET $setStr WHERE $condition";
echo $query;
return $this->execute($query);
}
function find($table=null, $fields=null, $condition="1=1", $order="1") {
if (null == $table || null == $fields) {
return FALSE;
}
$query = "SELECT $fields FROM $table WHERE $condition ORDER BY $order";
$result = $this->execute($query);
if (!$result) return $result;
$row = mysql_fetch_array($result, MYSQL_ASSOC);
mysql_free_result($result) or die('Error : Failed to free mysql result.');
return $row;
}
function findAll($table=null, $fields=null, $condition="1=1", $order="1") {
if (null == $table || null == $fields) return FALSE;
$query = "SELECT $fields FROM $table WHERE $condition ORDER BY $order";
$result = $this->execute($query);
if (!$result) return $result;
$rows = array();
while( $row = mysql_fetch_array($result, MYSQL_ASSOC) ) {
$rows[] = $row;
}
mysql_free_result($result) or die('Error : Failed to free mysql result.');
return $rows;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment