duandian4501 2017-01-27 12:47
浏览 55
已采纳

MySQL PDO准备语句

I'm trying out PHP and struggling with prepared statements. I've got the following code

<?php
require_once 'dbconfig.php';


try {
   $conn = new PDO("mysql:host=$host;dbname=$dbname", $username, $password);
   echo "Connected to $dbname at $host successfully.";
} catch (PDOException $pe) {
   die("Could not connect to the database $dbname :" . $pe->getMessage());
}

$statement = $conn->prepare("SELECT * FROM voicemessages WHERE dir = :dir");
$statement->bindParam("dir", "test");

$statement->execute();
$statement->setFetchMode(PDO::FETCH_ASSOC);
?>

But getting the following error:

Connected to voicemail1 at 192.168.1.121 successfully.
Warning: main(): It is not safe to rely on the system's timezone settings. You are *required* to use the date.timezone setting or the date_default_timezone_set() function. In case you used any of those methods and you are still getting this warning, you most likely misspelled the timezone identifier. We selected 'UTC' for 'GMT/0.0/no DST' instead in /var/lib/asterisk/agi-bin/KesherVoicemail.php on line 13

Fatal error: Cannot pass parameter 2 by reference in /var/lib/asterisk/agi-bin/MyScript.php on line 13

Line 13 is the bindParam line.

Please can someone explain what is wrong?

  • 写回答

2条回答 默认 最新

  • doushi8231 2017-01-27 12:51
    关注

    You need to pass parameter 2 by reference to the bindParam function.

    Replace this-

    $statement->bindParam("dir", "test");
    

    with this-

    $test = "test";
    $statement->bindParam(":dir", $test);
    

    Check out the doc

    variable: Name of the PHP variable to bind to the SQL statement parameter.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘