- mixed query ( string $query [, int $resultmode ] )
- <?php
- $mysqli=new mysqli("localhost","root","root","sunyang");
- //實例化mysqli
- $query="select * from employee";
- $result=$mysqli->query($query);
- if ($result) {
- if($result->num_rows>0){
- //判斷結果集中行的數目是否大於0
- while($row =$result->fetch_array() ){
- //循環輸出結果集中的記錄
- echo ($row[0])."<br>";
- echo ($row[1])."<br>";
- echo ($row[2])."<br>";
- echo ($row[3])."<br>";
- echo "<hr>";
- }
- }
- }else {
- echo "查詢失敗";
- }
- $result->free();
- $mysqli->close();
- ?>
圖23-24 使用query()方法的查詢結果 |
- <?php
- $mysqli=new mysqli("localhost","root","root","sunyang");
- //實例化mysqli
- $query="delete from employee where emp_id=2";
- $result=$mysqli->query($query);
- if ($result){
- echo "刪除操作執行成功";
- }else {
- echo "刪除操作執行失敗";
- }
- $mysqli->close();
- ?>
- <?php
- $mysqli=new mysqli("localhost","root","root","sunyang");
- //實例化mysqli
- $query="select * from employee";
- $result=$mysqli->prepare($query);
- //進行預準備語句查詢
- $result->execute();
- //執行預準備語句
- $result->bind_result($id,$number,$name,$age);
- //綁定結果
- while ($result->fetch()) {
- echo $id;
- echo $number;
- echo $name;
- echo $age;
- }
- $result->close();
- //關閉預準備語句
- $mysqli->close();
- //關閉連接
- ?>
- bool bind_param ( string $types , mixed &$var1 [, mixed &$... ] )
字符種類 | 代表的數據類型 |
I | integer |
D | double |
S | string |
B | blob |
- <?php
- $mysqli=new mysqli("localhost","root","root","sunyang");
- //實例化mysqli
- $query="insert into employee (emp_number,emp_name,emp_age)
- values (?,?,?)";
- $result=$mysqli->prepare($query);
- $result->bind_param("ssi",$number,$name,$age);
- //綁定參數
- $number='sy0807';
- $name='employee7';
- $age=20;
- $result->execute();
- //執行預準備語句
- $result->close();
- $mysqli->close();
- ?>
- <?php
- $mysqli=new mysqli("localhost","root","root","sunyang");
- //實例化mysqli
- $query="select * from employee where emp_id < ?";
- $result=$mysqli->prepare($query);
- $result->bind_param("i",$emp_id);
- //綁定參數
- $emp_id=4;
- $result->execute();
- $result->bind_result($id,$number,$name,$age);
- //綁定結果
- while ($result->fetch()) {
- echo $id."<br>";
- echo $number."<br>";
- echo $name."<br>";
- echo $age."<br>";
- }
- $result->close();
- $mysqli->close();
- ?>
- bool mysqli_multi_query ( mysqli $link , string $query )
- $mysqli=new mysqli("localhost","root","root","sunyang");
- //實例化mysqli
- $query = "select emp_name from employee ;";
- $query .= "select dep_name from depment ";
- if ($mysqli->multi_query($query)) {
- //執行多個查詢
- do {
- if ($result = $mysqli->store_result()) {
- while ($row = $result->fetch_row()) {
- echo $row[0];
- echo "<br>";
- }
- $result->close();
- }
- if ($mysqli->more_results()) {
- echo ("-----------------<br>");
- //連個查詢之間的分割線
- }
- } while ($mysqli->next_result());
- }
- $mysqli->close();//關閉連接
- ?>