国产探花免费观看_亚洲丰满少妇自慰呻吟_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);
?>

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 大连市| 华亭县| 道真| 赤城县| 德江县| 布尔津县| 富锦市| 怀来县| 乌海市| 邮箱| 江安县| 周宁县| 离岛区| 汉寿县| 清徐县| 临湘市| 额济纳旗| 台江县| 铜山县| 云南省| 肃宁县| 卫辉市| 玉溪市| 加查县| 永靖县| 乌兰浩特市| 万宁市| 亚东县| 九寨沟县| 镇宁| 开江县| 德庆县| 宣恩县| 丹棱县| 禄丰县| 铜山县| 乌鲁木齐县| 博罗县| 贺兰县| 巨野县| 满洲里市|