大年初七,在家无聊,写了个mysql操作类
浏览量:471 | 分类:PHP | 发布日期:2010-02-20
在家无聊,写了个简单的DB类,方便以后CURD;
以前用公司现成的DB类,现在自己写个;
支持Mysqli,高手见笑,本人还是菜鸟 ;
代码如下
-
<?php
-
-
class MysqlDB {
-
-
private $hostname;
-
private $username;
-
private $password;
-
private $db;
-
-
public function __construct() {
-
$num_args = func_num_args();
-
if($num_args > 0) {
-
$args = func_get_args();
-
$this->host = $args[0];
-
$this->user = $args[1];
-
$this->pass = $args[2];
-
}
-
$this->content();
-
}
-
-
private function mysqli_installed(){
-
if (function_exists ("mysqli_connect")){
-
return true;
-
} else {
-
return false;
-
}
-
}
-
-
private function content() {
-
try{
-
if($this->mysqli_installed()) {
-
if(!$this->db = new mysqli($this->host,$this->user,$this->pass)) {
-
$exceptionstring = "Error connection to database: <br />";
-
$exceptionstring .= mysqli_connect_errno() . ": " . mysqli_connect_error();
-
throw new exception ($exceptionstring);
-
}
-
}else{
-
if(!$this->db = new mysql_connect($this->host,$this->user,$this->pass)) {
-
$exceptionstring = "Error connection to database: <br />";
-
$exceptionstring .= mysql_errno() . ": " . mysql_error();
-
throw new exception ($exceptionstring);
-
}
-
}
-
} catch (exception $e) {
-
echo $e->getmessage();
-
}
-
}
-
-
public function select_db($db_name) {
-
try{
-
if($this->mysqli_installed() ) {
-
if(!$this->db->select_db($db_name)) {
-
$exceptionstring = "Error opening database: $db_name: <br />";
-
$exceptionstring .= $this->db->errno . ": " . $this->db->error;
-
throw new exception ($exceptionstring);
-
}
-
}else{
-
if(!mysql_select_db($db_name,$this->db)) {
-
$exceptionstring = "Error opening database: $db_name: <br />";
-
$exceptionstring .= mysql_errno() . ": " . mysql_error();
-
throw new exception ($exceptionstring);
-
}
-
}
-
} catch (exception $e) {
-
echo $e->getmessage();
-
}
-
}
-
-
public function execute($query) {
-
try{
-
if($this->mysqli_installed()) {
-
if(!$this->db->query($query)) {
-
$exceptionstring = "Error performing query: $query: <br />";
-
$exceptionstring .= $this->db->errno . ": " . $this->db->error;
-
throw new exception ($exceptionstring);
-
}
-
}else{
-
if(!mysql_query($query,$this->db)) {
-
$exceptionstring = "Error performing query: $query: <br />";
-
$exceptionstring .= mysql_errno() . ": " . mysql_error();
-
throw new exception ($exceptionstring);
-
}else{
-
echo "Query performed correctly: " . mysql_affected_rows () . " row(s) affected.<br />";
-
}
-
}
-
} catch (exception $e) {
-
echo $e->getmessage();
-
}
-
}
-
-
public function get_rows ($query){
-
try {
-
if ($this->mysqli_installed()){
-
if ($result = $this->db->query ($query)){
-
$returnarr = array ();
-
while ($adata = $result->fetch_array ()){
-
$returnarr = array_merge ($returnarr,$adata);
-
}
-
return $returnarr;
-
} else {
-
$exceptionstring = "Error performing query: $query: <br />";
-
$exceptionstring .= $this->db->errno . ": " . $this->db->error;
-
throw new exception ($exceptionstring);
-
}
-
} else {
-
if (!$aquery = mysql_query ($query)){
-
$exceptionstring = "Error performing query: $query: <br />";
-
$exceptionstring .= mysql_errno() . ": " . mysql_error();
-
throw new exception ($exceptionstring);
-
} else {
-
$returnarr = array ();
-
while ($adata = mysql_fetch_array ($aquery)){
-
$returnarr = array_merge ($returnarr,$adata);
-
}
-
return $returnarr;
-
}
-
}
-
} catch (exception $e) {
-
echo $e->getmessage();
-
}
-
}
-
-
public function affected_rows() {
-
if($this->mysqli_installed()) {
-
return $this->db->affected_rows;
-
}else{
-
return mysql_affected_rows ($this->db);
-
}
-
}
-
-
public function insert_id() {
-
if($this->mysqli_installed()) {
-
return $this->db->insert_id;
-
}else{
-
return mysql_insert_id ($this->db);
-
}
-
}
-
-
public function escape($str){
-
return trim(mysql_escape_string($str));
-
}
-
-
public function __destruct() {
-
try {
-
if ($this->mysqli_installed()){
-
if (!$this->db->close()){
-
$exceptionstring = "Error closing connection: <br />";
-
$exceptionstring .= $this->db->errno . ": " . $this->db->error;
-
throw new exception ($exceptionstring);
-
}
-
} else {
-
if (!mysql_close ($this->db)){
-
$exceptionstring = "Error closing connection: <br />";
-
$exceptionstring .= mysql_errno() . ": " . mysql_error();
-
throw new exception ($exceptionstring);
-
}
-
}
-
} catch (exception $e) {
-
echo $e->getmessage();
-
}
-
}
-
-
}
-
-
-
/*test*/
-
$mysql = new MysqlDB('localhost','root','');
-
-
$mysql->select_db('test');
-
-
$update = $mysql->execute ("UPDATE test SET usereMail='test@test.com' WHERE id='1'");
-
-
$insert = $mysql->execute ("INSERT INTO test SET usereMail = 'test@test.com'");
-
-
$insert_id = $mysql->insert_id($insert);
-
-
$test = $mysql->get_rows("SELECT * FROM `test` WHERE 1");
-
-
$affected_rows = $mysql->affected_rows($insert);
-
-
-
?>
上一篇: 大年初二
下一篇: 最简单的PHP模板引擎
过客 2010-02-21 00:00:43
深藏不露啊,你牛逼!我服你了