mysqli和mysqli_result能完成的功能 均可以使用mysqli_stmt類開完成
1.編譯一次,使用屢次,相似於存儲過程
2.參數化查詢,可防止sql注入php
1: <?php
2: header("Content-Type:text/html; charset=utf8");
3:
4: $mysqli = new mysqli("localhost","root","1234","test2");
5:
6: if($mysqli->connect_error)
7: {
8: die("鏈接數據庫出錯:".$mysqli->connect_error);
9: }
10:
11:
12: // 增
13: // $sql = "insert into userinfo(uName,uAge,uPwd) values(?,?,?);";
14: // 刪
15: // $sql = "delete from userinfo where id=?;";
16: // 改
17: // $sql = "update userinfo set uAge=? where Id=?;";
18: // 查
19: // $sql = "select top(?) uName,uAge,uPwd from userinfo where id>?;";
20: $sql = "select uname,uage,upwd from test2.userinfo where id>? limit ?,5";
21:
22:
23: //建立預編譯對象
24: $stmt=$mysqli->prepare($sql);
25:
26: //按順序給點位符綁定值(綁定參數)
27: //s:string,i:int,d:double,b:二進制大數據類型
28:
29: // 增
30: // $stmt->bind_param("sis",$uName,$uAge,$uPwd);
31: // $uName="阿斯頓";
32: // $uAge=28;
33: // $uPwd=3557;
34:
35: // 刪
36: // $stmt->bind_param("i",$Id);
37: // $Id=25;
38:
39: // 改
40: // $stmt->bind_param("ii",$uAge,$Id);
41: // $uAge=15;
42: // $Id=26;
43:
44: // 查
45: $stmt->bind_param("ii",$Id,$limitNum);
46: $Id=10;
47: $limitNum=5;
48:
49: $stmt->bind_result($uName,$uAge,$uPwd);
50:
51: //執行
52: $result = $stmt->execute();
53: if(!$result) echo "執行語句出錯:".$stmt->error;
54:
55: while ($stmt->fetch()) {
56: echo "$uName----$uAge----$uPwd\n";
57: }
58:
59: // echo "最後一次添加的數據ID:".$stmt->insert_id."\n";
60: // echo "受影響行數".$stmt->affected_rows."\n";//只返回最後一次執行sql受影響的行數
61:
62: $stmt->close();
63:
64:
65:
66: ?>