刚刚学到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"));
------解决方案--------------------
引用手册中的话
你直接写成字符串是不能引用传递的。