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

首頁 > 語言 > PHP > 正文

PHP封裝mysqli基于面向?qū)ο蟮膍ysql數(shù)據(jù)庫操作類與用法示例

2024-05-05 00:06:57
字體:
供稿:網(wǎng)友

本文實(shí)例講述了PHP封裝mysqli基于面向?qū)ο蟮膍ysql數(shù)據(jù)庫操作與用法。分享給大家供大家參考,具體如下:

首先封裝好mysql類

mysql.php

<?phpclass Mysql{     private static $host="localhost";     private static $user="root";     private static $password="123456";     private static $dbName="test";           //數(shù)據(jù)庫名     private static $charset="utf8";          //字符編碼     private static $port="3306";            //端口號(hào)     private $conn=null;     function __construct(){       $this->conn=new mysqli(self::$host,self::$user,self::$password,self::$dbName,self::$port);       if(!$this->conn)       {          die("數(shù)據(jù)庫連接失敗!".$this->conn->connect_error);       }else{         echo "連接成功!";       }       $this->conn->query("set names ".self::$charset);     }     //執(zhí)行sql語句     function sql($sql){       $res=$this->conn->query($sql);     if(!$res)       {          echo "數(shù)據(jù)操作失敗";       }       else       {          if($this->conn->affected_rows>0)          {             return $res;          }          else          {            echo "0行數(shù)據(jù)受影響!";          }       }     }     //返回受影響數(shù)據(jù)行數(shù)     function getResultNum($sql){      $res=$this->conn->query($sql);      return mysqli_num_rows($res);      }     //關(guān)閉數(shù)據(jù)庫     public function close()     {       @mysqli_close($this->conn);     }}?>

然后就可以調(diào)用了

index.php

<?phprequire_once "mysql.php";$conn=new Mysql();$sql="select * from user";//執(zhí)行查詢并獲取查詢結(jié)果$result=$conn->sql($sql);//輸出受影響數(shù)據(jù)行數(shù)$num=$conn->getResultNum($sql);echo "影響的行數(shù):".$num;//讀取并輸出記錄while ($row = mysqli_fetch_assoc($result)){  echo "{$row['name']} ";  echo "{$row['password']}";}//關(guān)閉數(shù)據(jù)庫$conn->close();

希望本文所述對(duì)大家PHP程序設(shè)計(jì)有所幫助。


注:相關(guān)教程知識(shí)閱讀請(qǐng)移步到PHP教程頻道。
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表

圖片精選

主站蜘蛛池模板: 梁河县| 东平县| 含山县| 丹江口市| 白玉县| 黑河市| 察隅县| 古交市| 渝北区| 贺州市| 宁晋县| 江西省| 邵阳市| 盖州市| 宝山区| 湖州市| 绥芬河市| 灵璧县| 昭通市| 浦城县| 顺平县| 屯昌县| 大安市| 临朐县| 田阳县| 垫江县| 新疆| 江达县| 罗田县| 青州市| 吴桥县| 巨鹿县| 新津县| 长乐市| 和林格尔县| 武邑县| 泰来县| 漳浦县| 黄山市| 邵阳县| 达拉特旗|