php實(shí)現(xiàn)mysql封裝類示例
php封裝mysql類
<?php
class Mysql {
private $host;
private $user;
private $pwd;
private $dbName;
private $charset;
private $conn = null;
public function __construct() {
$this->host = 'localhost';
$this->user = 'root';
$this->pwd = 'root';
$this->dbName = 'test';
$this->connect($this->host,$this->user,$this->pwd);
$this->switchDb($this->dbName);
$this->setChar($this->charset);
}
//負(fù)責(zé)鏈接
private function connect($h,$u,$p) {
$conn = mysql_connect($h,$u,$p);
$this->conn = $conn;
}
//負(fù)責(zé)切換數(shù)據(jù)庫(kù)
public function switchDb($db) {
$sql = 'use' . $db;
$this->query($sql);
}
//負(fù)責(zé)設(shè)置字符集
public function setChar($char) {
$sql = 'set names' . $char;
$this->query($sql);
}
//負(fù)責(zé)發(fā)送sql查詢
public function query($sql) {
return mysql_query($sql,$this->conn);
}
//負(fù)責(zé)獲取多行多列的select結(jié)果
public function getAll($sql) {
$list = array();
$rs = $this->query($sql);
if (!$rs) {
return false;
}
while ($row = mysql_fetch_assoc($rs)) {
$list[] = $row;
}
return $list;
}
public function getRow($sql) {
$rs = $this->query($sql);
if(!$rs) {
return false;
}
return mysql_fetch_assoc($rs);
}
public function getOne($sql) {
$rs = $this->query($sql);
if (!$rs) {
return false;
}
return mysql_fetch_assoc($rs);
return $row[0];
}
public function close() {
mysql_close($this->conn);
}
}
echo '<pre>';
$mysql = new Mysql();
print_r($mysql);
$sql = "insert into stu values (4,'wangwu','99998')";
if($mysql->query($sql)){
echo "query成功";
}else {
echo "失敗";
}
echo "<br />";
$sql = "select * from stu";
$arr = $mysql->getAll($sql);
print_r($arr);
?>
- PHP封裝mysqli基于面向?qū)ο蟮膍ysql數(shù)據(jù)庫(kù)操作類與用法示例
- PHP封裝的mysqli數(shù)據(jù)庫(kù)操作類示例
- PHP基于MySQLI函數(shù)封裝的數(shù)據(jù)庫(kù)連接工具類【定義與用法】
- php基于PDO實(shí)現(xiàn)功能強(qiáng)大的MYSQL封裝類實(shí)例
- php基于單例模式封裝mysql類完整實(shí)例
- php封裝的mysqli類完整實(shí)例
- php mysql 封裝類實(shí)例代碼
- php封裝的連接Mysql類及用法分析
- PHP訪問MYSQL數(shù)據(jù)庫(kù)封裝類(附函數(shù)說明)
- php鏈?zhǔn)讲僮鱩ysql數(shù)據(jù)庫(kù)(封裝類帶使用示例)
相關(guān)文章
Zend Framework教程之Zend_Config_Ini用法分析
這篇文章主要介紹了Zend Framework教程之Zend_Config_Ini用法,較為詳細(xì)的分析了Zend_Config_Ini操作配置數(shù)據(jù)的相關(guān)技巧,需要的朋友可以參考下2016-03-03
Joomla調(diào)用系統(tǒng)自帶編輯器的實(shí)現(xiàn)方法
這篇文章主要介紹了Joomla調(diào)用系統(tǒng)自帶編輯器的實(shí)現(xiàn)方法,實(shí)例分析了Joomla調(diào)用系統(tǒng)自帶編輯器的具體步驟、相關(guān)函數(shù)與使用技巧,需要的朋友可以參考下2016-05-05
yii實(shí)現(xiàn)model添加默認(rèn)值的方法(2種方法)
這篇文章主要介紹了yii實(shí)現(xiàn)model添加默認(rèn)值的方法,結(jié)合實(shí)例分析了在rules()方法及在beforeSave()方法中設(shè)定兩種實(shí)現(xiàn)技巧,需要的朋友可以參考下2016-01-01
thinkPHP5框架實(shí)現(xiàn)分頁查詢功能的方法示例
這篇文章主要介紹了thinkPHP5框架實(shí)現(xiàn)分頁查詢功能的方法,結(jié)合實(shí)例形式分析了thinkPHP5實(shí)現(xiàn)分頁查詢功能的相關(guān)控制器、模板等操作技巧,需要的朋友可以參考下2018-03-03

