php bind_param問題,新手求教
剛剛學到prepared這兒,敲了如下代碼
$db = new mysqli("localhost", "xx","xxxxxxx","books");
$insert_str = "insert into customers(name,address,city) values(?,?,?)";
$stmt = $db->prepare($insert_str);
$stmt->bind_param("sss","john","street one","beijing");
$stmt->execute();
echo $stmt->affected_rows;
然後運行,報錯
Cannot pass parameter 2 by reference
然後根據網上的代碼改成
$db = new mysqli("localhost", "xx","xxxxxx","books");
$insert_str = "insert into customers(name,address,city) values(?,?,?)";
$stmt = $db->prepare($insert_str);
$name ="john";
$address = "street one";
$city = "beijing";
$stmt->bind_param("sss",$name,$address,$city);
$stmt->execute();
echo $stmt->affected_rows;
顯示插入資料成功,就想問問第一個版本到底錯在哪
------解決方案--------------------
bind_param 的第二個參數起傳遞的是引用
你直接寫成字串,這是在 php 5.3 及以後是不允許的
其實你可以不要
$stmt->bind_param("sss","john","street one","beijing");
而直接寫成
$stmt->execute(array("john","street one","beijing"));
------解決方案--------------------
引用手冊中的話
引用
mysqli_stmt::bind_param -- mysqli_stmt_bind_param — Binds variables to a prepared statement as parameters
Note that mysqli_stmt_bind_param() requires parameters to be passed by reference
你直接寫成字串是不能引用傳遞的。