mysql綁定變量的使用方法


<?php
$mysqli = new mysqli("localhost", "my_user", "my_password", "world");

/* check connection */
if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
}

$city = "Amersfoort";

/* create a prepared statement */
if ($stmt = $mysqli->prepare("SELECT District FROM City WHERE Name=?")) {

    /* bind parameters for markers */
    $stmt->bind_param("s", $city);

    /* execute query */
    $stmt->execute();

    //下面的變量為查詢表中的字段命名的變量
    $stmt->bind_result($district);

    /* fetch value */
    $stmt->fetch();

    printf("%s is in district %s\n", $city, $district);

    /* close statement */
    $stmt->close();
}

/* close connection */
$mysqli->close();
?>

 上面的是mysql中查詢的變量幫定使用方法

在mysql的查詢中需要將語句先進行解析處理再進行緩存處理,如果在解析的過程中判斷該語句已經執行過的話則直接執行,不會再次進行解析

如下面的語句

select * from flight where fnum='MU5674'

select * from fllight where fnum='MU5655'

可能因為一個變量的原因就需要mysql的重新解析 這時就需要mysql的重新解析

如果寫成

select * from flight where fnum=?

則此時的效果就不一樣了  執行的結果可以通過

mysql> show profiles;
+----------+------------+-------------------------------------------+
| Query_ID | Duration   | Query                                     |
+----------+------------+-------------------------------------------+
|       31 | 0.00007200 | set @s:=3                                 |
|       32 | 0.01539800 | select count(*) from io_01 where c1 = @s  |
|       33 | 0.01528700 | select count(*) from io_01 where c1 = @s  |
|       34 | 0.01539700 | select count(*) from io_01 where c1 = @s  |
|       35 | 0.01587500 | select count(*) from io_01 where c1 = @s  |
|       36 | 0.01508000 | select count(*) from io_01 where c1 = @s  |
|       37 | 0.01512300 | select count(*) from io_01 where c1 = @s  |
|       38 | 0.01506700 | select count(*) from io_01 where c1  =  3 |
|       39 | 0.00022400 | select count(*) from io_01 where c1  =  3 |
|       40 | 0.00021400 | select count(*) from io_01 where c1  =  3 |
|       41 | 0.00004000 | select count(*) from io_01 where c1  =  3 |
|       42 | 0.00022300 | select count(*) from io_01 where c1  =  3 |
|       43 | 0.00023800 | select count(*) from io_01 where c1  =  3 |
|       44 | 0.00022500 | select count(*) from io_01 where c1  =  3 |
|       45 | 0.00004000 | select count(*) from io_01 where c1  =  3 |
+----------+------------+-------------------------------------------+
15 rows in set (0.00 sec)

來查詢執行的結果 !

以下是mysql綁定變量在增刪改方面的運用

<?php 
                $mysqli=new mysqli("localhost", "root", "123321", "test"); 
 
                $sql1="set @@profiling=1"; 
                $result1=$mysqli->query($sql1); 
 
                //准備好一條語句放到服務器中,插入語句 
                $sql="insert into t(name,sex) values (?,?)"; 
 
                $stmt=$mysqli->prepare($sql); 
 
                //給占位符號每個?號傳值(綁定參數) i    d    s    b    
                $stmt->bind_param("si", $name, $sex); 
 
                $name="andy"; 
                $sex=0; 
 
                //執行 
                $stmt->execute(); 
 
 
                $name="mandy"; 
                $sex=1; 
 
                //執行 
                $stmt->execute(); 
 
 
                $name="michael"; 
                $sex=0; 
 
                //執行 
                $stmt->execute(); 
 
 
                $name="happy"; 
                $sex=1; 
 
                //執行 
                $stmt->execute(); 
 
                $name="php"; 
                $sex=1; 
 
                //執行 
                $stmt->execute(); 
 
 
                $name="mysql"; 
                $sex=1; 
 
                //執行 
                $stmt->execute(); 
 
                $name="linux"; 
                $sex=1; 
 
                //執行 
                $stmt->execute(); 
 
                $name="oracle"; 
                $sex=1; 
 
                //執行 
                $stmt->execute(); 
 
                $name="unix"; 
                $sex=1; 
 
                //執行 
                $stmt->execute(); 
 
                $name="cisco"; 
                $sex=1; 
 
                //執行 
                $stmt->execute(); 
 
 
                $stmt->close(); 
 
 
                $sql2="show profiles"; 
                $result2=$mysqli->query($sql2); 
 
                echo '<table border=1 align="center" width=800>'; 
                while($rows=$result2->fetch_assoc()){ 
                                echo '<tr align="center">'; 
                                foreach($rows as $value){ 
                                                echo '<td>' . $value    . '</td>'; 
                                } 
                                echo '</tr>'; 
                                $i=0; 
                                $i=$i+$rows["Duration"]; 
                } 
                echo '</table>'; 
                echo $i; 
?>

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM