php bind_param有關問題,新手求教

來源:互聯網
上載者:User
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


你直接寫成字串是不能引用傳遞的。


  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.