前言
在實際開發中,關於數據庫操做類,不多是本身去寫,大可能是經過一些框架去實現,忽然本身去寫,仍是須要借閱手冊之類,因而我以爲有必要去總結一下,php鏈接mysql的方法,php鏈接mysql,能夠經過mysql擴展、mysqli擴展,pdo擴展,由於高版本的php將移除mysql_系列方法,故在此只總結另外兩種鏈接方式。首先咱們得確保php的這兩個擴展是否已經打開,查看php.ini配置文件以下:javascript
提示,若有有的人說,我打開了擴展庫(即去掉了,前面的';'),仍然一直提示mysqli_或pdo系列方法找不到,這極可能是由於你沒有指定擴展庫所在目錄。找到extension_dir 參數,指定擴展所在目錄便可php
extension_dir = "D:/wamp/bin/php/php5.5.12/ext/"java
配置文件
首先咱們將鏈接數據庫須要的配置文件,單獨出來,這樣就不須要每次都去寫,須要的時候,直接include或者require包含進來就能夠了。若是關於include與require不清楚的,能夠查考http://blog.csdn.net/hsd2012/article/details/51089785mysql
文件名conf.phpsql
- return array(
- 'host'=>'127.0.0.1',
- 'user'=>'root',
- 'password'=>'',
- 'dbName'=>'xxpt',
- 'charSet'=>'utf8',
- 'port'=>'3306'
);
經過mysqli擴展鏈接
mysqli有兩種方式去鏈接mysql,且支持預處理,一種是面向對象,一種是面向過程。數據庫
1.面向過程鏈接mysql
- $dbConf=include 'conf.php';
- function openDb($dbConf){
- $conn=mysqli_connect($dbConf['host'],$dbConf['user'],$dbConf['password'],$dbConf['dbName'],$dbConf['port']) or die('打開失敗');
-
- mysqli_set_charset($conn,$dbConf['charSet']);
- return $conn;
- }
- function closeDb($conn){
- mysqli_close($conn);
- }
-
- $conn=openDb($dbConf);
- $sql='SELECT t.`id1` from `t1` as t';
- $rs=$conn->query($sql);
- $data=array();
- while($tmp=mysqli_fetch_assoc($rs)){
- $data[]=$tmp;
- }
- print_r($data);
-
- $sql='INSERT INTO `t1`(`id1`,`id2`) VALUES(3,4);';
- $rs=$conn->query($sql);
- closeDb($conn);
2.面向對象方式鏈接mysql
- $dbConf=include 'conf.php';
- $conn=new mysqli($dbConf['host'],$dbConf['user'],$dbConf['password'],$dbConf['dbName'],$dbConf['port']);
- if(!$conn){
- die('數據庫打開失敗');
- }
- $sql='SELECT t.`id1` from `t1` as t';
- $rs=$conn->query($sql);
- while ($tmp=$rs->fetch_assoc()) {
- print_r($tmp);
- }
- $sql='DELETE FROM `t1` WHERE `id1`=3';
- $rs=$conn->query($sql);
- print_r($rs);$conn->close();
3.mysqli預處理
主要講解mysli對象編程的預處理,至於面向過程變成的預處理使用mysqli_prepare就不在介紹
$dbConf=include 'conf.php';
- $conn=new mysqli($dbConf['host'],$dbConf['user'],$dbConf['password'],$dbConf['dbName'],$dbConf['port']);
- if(!$conn){
- die('數據庫打開失敗');
- }
- $sql='SELECT * from `t1` as t WHERE id2>?';
- $stmt=$conn->prepare($sql);
- if(!$stmt){
- die('sql語句有問題');
- }
- $id2=2;
- $stmt->bind_param('i',$id2);
- $stmt->execute();
- $stmt->bind_result($id1, $id2);
- while ($tmp=$stmt->fetch()) {
- print_r('id1='.$id1.',id2='.$id2);
- echo '</br>';
- }
- $stmt->free_result();
- $stmt->close();
- $conn->close();
預處理綁定參數中參數類型說明以下
php使用PDO方式鏈接mysql
- $dbConf=include 'conf.php';
- $pdo=myPDO::getInstance($dbConf);
- $sql='SELECT t.`id1` from `t1` as t';
- $rs=$pdo->query($sql);
- $data=$rs->fetchAll();
- print_r($data);
- $sql='UPDATE t1 SET t1.`id1`=11 WHERE t1.`id1`=1';
- $rs=$pdo->query($sql);
-
- class myPDO{
- private static $pdo;
-
- private function __construct(){
-
- }
- private function __clone(){
-
- }
-
- public static function getInstance($dbConf){
- if(!(self::$pdo instanceof PDO)){
- $dsn ="mysql:host=".$dbConf['host'].";port=".$dbConf['port'].";dbname=".$dbConf['dbName'].";charset=".$dbConf['charSet'];
- try {
- self::$pdo = new PDO($dsn,$dbConf['user'], $dbConf['password'], array(PDO::ATTR_PERSISTENT => true,PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8"));
- self::$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING);
- } catch (PDOException $e) {
- print "Error:".$e->getMessage()."<br/>";
- die();
- }
- }
- return self::$pdo;
- }
- }
pdo支持預處理,推薦使用預處理方式,以防sql注入。