国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > PHP > 正文

php實現mysql封裝類示例

2024-05-04 23:23:22
字體:
來源:轉載
供稿:網友

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);
 }

 //負責鏈接
 private function connect($h,$u,$p) {
  $conn = mysql_connect($h,$u,$p);
  $this->conn = $conn;
 }

 //負責切換數據庫
 public function switchDb($db) {
  $sql = 'use' . $db;
  $this->query($sql);
 }

 //負責設置字符集
 public function setChar($char) {
  $sql = 'set names' . $char;
  $this->query($sql);
 }

 //負責發送sql查詢
 public function query($sql) {
  return mysql_query($sql,$this->conn);
 }

 //負責獲取多行多列的select結果
 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);
?>

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 陇西县| 德江县| 阿巴嘎旗| 兴海县| 温州市| 化德县| 左贡县| 普格县| 体育| 甘泉县| 平陆县| 丰顺县| 赤水市| 九寨沟县| 道孚县| 巩义市| 东海县| 长顺县| 友谊县| 宽城| 梓潼县| 中牟县| 永寿县| 松江区| 肃宁县| 郴州市| 新巴尔虎右旗| 凤冈县| 大埔区| 乌兰察布市| 绵竹市| 兴安盟| 罗源县| 衡阳市| 梁河县| 大石桥市| 乌审旗| 灵丘县| 永城市| 鹤岗市| 宜昌市|