Home  >  Article  >  Backend Development  >  php bind_param有关问题,新手求教

php bind_param有关问题,新手求教

WBOY
WBOYOriginal
2016-06-13 12:04:031557browse

php bind_param问题,新手求教
刚刚学到prepared这儿,敲了如下代码

<br />$db = new mysqli("localhost", "xx","xxxxxxx","books");<br />    $insert_str = "insert into customers(name,address,city) values(?,?,?)";<br />    $stmt = $db->prepare($insert_str);<br />    $stmt->bind_param("sss","john","street one","beijing");<br />    $stmt->execute();<br />    echo $stmt->affected_rows;<br />

然后运行,报错
 Cannot pass parameter 2 by reference
然后根据网上的代码改成
<br />$db = new mysqli("localhost", "xx","xxxxxx","books");<br />    $insert_str = "insert into customers(name,address,city) values(?,?,?)";<br />    $stmt = $db->prepare($insert_str);<br />    $name ="john";<br />    $address = "street one";<br />    $city = "beijing";<br />    $stmt->bind_param("sss",$name,$address,$city);<br />    $stmt->execute();<br />    echo $stmt->affected_rows;<br />

显示插入数据成功,就想问问第一个版本到底错在哪
------解决方案--------------------
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


你直接写成字符串是不能引用传递的。


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn