php實現(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ù)庫
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ù)庫操作類與用法示例
- PHP封裝的mysqli數(shù)據(jù)庫操作類示例
- PHP基于MySQLI函數(shù)封裝的數(shù)據(jù)庫連接工具類【定義與用法】
- php基于PDO實現(xiàn)功能強大的MYSQL封裝類實例
- php基于單例模式封裝mysql類完整實例
- php封裝的mysqli類完整實例
- php mysql 封裝類實例代碼
- php封裝的連接Mysql類及用法分析
- PHP訪問MYSQL數(shù)據(jù)庫封裝類(附函數(shù)說明)
- php鏈?zhǔn)讲僮鱩ysql數(shù)據(jù)庫(封裝類帶使用示例)
相關(guān)文章
基于php實現(xiàn)隨機合并數(shù)組并排序(原排序)
最近做了一個項目,其中有這樣一個需求要實現(xiàn),原有帖子列表A,現(xiàn)在需要在A中推廣新業(yè)務(wù)B,那么需要在A列表中1:1混合B中的數(shù)據(jù),隨機混合,但是需要保持A和B兩列原來的數(shù)據(jù)排序,本篇文章給大家介紹基于php實現(xiàn)隨機合并數(shù)組并排序(原排序),需要的朋友參考下2015-11-11基于PHP代碼實現(xiàn)中獎概率算法可用于刮刮卡、大轉(zhuǎn)盤等抽獎算法
大轉(zhuǎn)盤中獎概率算法在我們的日常生活中,經(jīng)常遇到,那么基于php代碼是如何實現(xiàn)中獎概率算法的,下面通過一段代碼實例給大家介紹php中獎概率算法2015-12-12php使用workman框架實現(xiàn)socket服務(wù)以及連接客戶端
這篇文章主要介紹了php使用workman框架實現(xiàn)socket服務(wù)以及連接客戶端,本文列舉了詳細(xì)的過程和代碼展示,能夠幫助你學(xué)習(xí),需要的朋友可以參考下2021-06-06基于OpenCart 開發(fā)支付寶,財付通,微信支付參數(shù)錯誤問題
使用OpenCart 開發(fā)支付寶,財付通,微信支付如果稍不用心,很容易導(dǎo)致很多問題,而這些錯誤很有可能是參數(shù)傳遞錯誤,下面小編給大家整理了一篇文章很不錯,在此分享給大家,感興趣的朋友一起看看吧2015-10-10