[PHP] - mysql 數據庫操做

使用PHP操做數據庫有兩種方式php

  1. 使用mysql_XXXX()方法
    1. 使用這種方式,須要先把php.ini裏的extension=php_mysql.dll去掉註釋
  2. 使用PDO
    1. 使用這種試,須要把php.ini裏的extension=php_pdo_mysql.dll去掉註釋

 

下面演示使用第一種方式:html

<!doctype html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    </head>
    <body>
       <?php
           $conn = mysql_connect("localhost", "root", "XXXXXX");
           if(!$conn) {
               die("Could not connect:" . mysql_error());
           }
           
           mysql_select_db("test", $conn);
           //mysql_query("SET NAMES utf8");
           
           $result = mysql_query("INSERT INTO mytable(headline, create_time) VALUES('中國', '" . date("Y-m-d h:i:s") . "');");
           if( $result < 1) {
               echo "insert error!";
           }
           
           $query = mysql_query("SELECT * FROM mytable LIMIT 100 OFFSET 0;");
           while ($row = mysql_fetch_array($query, MYSQL_BOTH)) {
               echo "<p>", $row["id"], " - " , $row["headline"], " - ", $row["create_time"], "</p>";
           }
           
           mysql_close();
       ?>
    </body>
</html>

 

下面是使用PDO方式:mysql

參數引用:sql

http://php.ncong.com/mysql/pdo/pdo_huoqu.html數據庫

 

<!doctype html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    </head>
    <body>
       <?php
           try {
               $pdo = new PDO("mysql:host=localhost;dbname=test", "root", "XXXXXX");
               //設置錯誤使用異常的模式
               $pdo -> setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
               //關閉自動提交
               //$pdo-> setAttribute(PDO::ATTR_AUTOCOMMIT, 0);
           } catch (PDOException $e) {
               echo sprintf("Exception message=%s", $e->getMessage());
               exit();
           }
           
           /**
            * 防SQL注入方式條件查詢
            */
           $stmt = $pdo->prepare('SELECT * FROM mytable WHERE id = :id');
           $stmt->execute(array(":id"=>1));
           foreach ($stmt as $row) {
               echo $row["headline"];
           }
           
           /**
            * 插入數據
            */
           $result = $pdo->exec("INSERT INTO mytable(headline, create_time) VALUES('中國', '" . date("Y-m-d h:i:s") . "');");
           if($result) {
               $str = sprintf("add data completed, lastupdateid=%s", $pdo->lastInsertId());
               echo $str;
           }
           
           /**
            * 查詢
            */
           echo "<hr/>查詢";
           $rs = $pdo->query("SELECT * FROM mytable");
           while ($row = $rs->fetch()) {
               echo "<p>", $row["id"], " - " , $row["headline"], " - ", $row["create_time"], "</p>";
           }

           /**
            * 字段映射方式查詢
            */
           echo "<hr/>字段映射方式查詢";
           $q = $pdo->query("SELECT id, headline, create_time FROM mytable");
           while (list($id, $headline, $createTime) = $q->fetch(PDO::FETCH_NUM)) {
               echo "<p>", $id, " - " , $headline, " - ", $createTime, "</p>";
           }
           
           /**
            * 一次性查詢方式
            */
           echo "<hr/>一次性查詢方式";
           $query = $pdo->prepare("SELECT * FROM mytable");
           $query->execute();
           $rows = $query->fetchAll(PDO::FETCH_ASSOC);
           foreach ($rows as $row) {
               echo "<p>", $row["id"], " - " , $row["headline"], " - ", $row["create_time"], "</p>";
           }
           
           /**
            * 字段綁定方式
            */
           echo "<hr/>字段綁定方式";
           $stm = $pdo->prepare("SELECT id, headline, create_time FROM mytable");
           $stm->execute();
           $stm->bindColumn(1, $id);
           $stm->bindColumn("headline", $headline);
           $stm->bindColumn(3, $createTime);
           while ($stm->fetch(PDO::FETCH_BOUND)) {
               echo "<p>", $id, " - " , $headline, " - ", $createTime, "</p>";
           }
           
           //$pdo-> setAttribute(PDO::ATTR_AUTOCOMMIT, 1);
       ?>
    </body>
</html>

 


 

建議使用PDO方式,這樣能夠減小SQL注入安全性問題。(php5以上建議使用PDO方式作數據庫操做)安全

相關文章
相關標籤/搜索