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 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?