新自學php,書上一直沒有好的例子,網上的也有錯誤,自己照著php手冊寫了一個完整點的
高手幫我看一下,有沒有哪裡寫錯了...謝謝了
<code>//面向对象 $mysqli = new mysqli("localhost", "user", "pass", "database"); if(mysqli_connect_errno())) { //这边判断方式和下面面向过程中的直接判断变量有什么区别? printf("Connect failed: %s\n", mysqli_connect_error()); exit(); } $query = "SELECT `name`, `address`, `phone` FROM Users WHERE gender=? AND age=?"; if($stmt = $mysqli->prepare($query) { $stmt->bind_param("si", $gender, $age); //这边插入参数是不是写在这里? $stmt->execute(); $stmt->bind_result($name, $address, $phone); while($stmt->fetch()) { printf ("%s (%s,%s)<br />", $name, $address, $phone); } $stmt->close(); } $mysqli->close(); /面向过程 $mysqli = mysqli_connecti("localhost", "user", "pass", "database"); if(!$mysqli) { printf ("Connect failed: %s\n", mysqli_connect_error()); exit(); } $query = "SELECT `name`, `address`, `phone` FROM Users WHERE gender=? AND age=?"; if ($stmt = mysqli_prepare($query)){ mysqli_stmt_bind_param($stmt, "si", $gender, $age); mysqli_stmt_execute($stmt); mysqli_stmt_bind_result($stmt, $name, $address, $phone); while (mysqli_stmt_fetch($stmt)) { printf ("%s (%s,%s)<br />", $name, $address, $phone); } mysqli_stmt_close($stmt); } $mysqli_close();</code>
新自學php,書上一直沒有好的例子,網上的也有錯誤,自己照著php手冊寫了一個完整點的
高手幫我看一下,有沒有哪裡寫錯了...謝謝了
<code>//面向对象 $mysqli = new mysqli("localhost", "user", "pass", "database"); if(mysqli_connect_errno())) { //这边判断方式和下面面向过程中的直接判断变量有什么区别? printf("Connect failed: %s\n", mysqli_connect_error()); exit(); } $query = "SELECT `name`, `address`, `phone` FROM Users WHERE gender=? AND age=?"; if($stmt = $mysqli->prepare($query) { $stmt->bind_param("si", $gender, $age); //这边插入参数是不是写在这里? $stmt->execute(); $stmt->bind_result($name, $address, $phone); while($stmt->fetch()) { printf ("%s (%s,%s)<br />", $name, $address, $phone); } $stmt->close(); } $mysqli->close(); /面向过程 $mysqli = mysqli_connecti("localhost", "user", "pass", "database"); if(!$mysqli) { printf ("Connect failed: %s\n", mysqli_connect_error()); exit(); } $query = "SELECT `name`, `address`, `phone` FROM Users WHERE gender=? AND age=?"; if ($stmt = mysqli_prepare($query)){ mysqli_stmt_bind_param($stmt, "si", $gender, $age); mysqli_stmt_execute($stmt); mysqli_stmt_bind_result($stmt, $name, $address, $phone); while (mysqli_stmt_fetch($stmt)) { printf ("%s (%s,%s)<br />", $name, $address, $phone); } mysqli_stmt_close($stmt); } $mysqli_close();</code>
程式碼是寫出來跑的,程式碼寫的對不對,得問編譯器或解析器,它們才是真正的老師。
你自己跑一遍呀。 。 。
這肉眼真不好看,最後一句好像有問題, 得傳入參數($mysqli)吧.
prepare+execute後bind_result+循環fetch是傳統的做法.
PHP從5.4開始底層預設採用mysqlnd實作mysql,mysqli,pdo_mysql.
這時你可以使用get_result 和fetch_all 快速拿到查詢結果. $stmt->get_result()->fetch_all(MYSQLI_ASSOC)
即可拿到結果集.例如:
<code>post.php?id=1024 MySQLi普通查询: $db = new mysqli('127.0.0.1','user','pass','dbname',3306); var_export($db->query('SELECT * FROM posts WHERE id='.intval($_GET['id']))->fetch_all()); MySQLi预处理绑定参数查询: $db = new mysqli('127.0.0.1','user','pass','dbname',3306); $stmt = $db->prepare('SELECT * FROM posts WHERE id=?'); //预处理 $stmt->bind_param('i', $_GET['id']); //绑定参数(可以防止SQL注入) $stmt->execute(); //查询 var_export($stmt->get_result()->fetch_all());</code>