dongyuan1984 2016-02-17 14:30
浏览 43
已采纳

PHP PDO - 使用预准备语句会导致服务器错误

I have a PHP website that I've spend some time developing using a XAMPP localhost environment. It works great when I test it locally but unfortunately the same can not be said for when I try running it on the web server space that I've rented.

When I uploaded my finished site and tried visiting it I was greeted with 'server error 500' and after some hours of bug-hunting frustration I have pinpointed what causes the error. It turned out that executing prepared statement causes the server errors, and I have no idea why. The following code will cause a server error:

try {$db = new PDO("mysql:host=hostname;dbname=dbname", "username", "password");}
catch(PDOException $e) {echo $e->getMessage();}
$temp = $db->prepare("SELECT * FROM users WHERE username=?");
$temp->execute(["theusername"]);  //It's this line that causes the error.

The following code does the same thing but without a prepared statement; it works without any problems with the web host:

try {$db = new PDO("mysql:host=hostname;dbname=dbname", "username", "password");}
catch(PDOException $e) {echo $e->getMessage();}
$temp = $db->query("SELECT * FROM users WHERE username='username'");

Both of them work fine when run locally with XAMPP. Anyone have any idea why the web host gives me server errors if I try to use prepared statements?

Edit:

My XAMPP installation runs PHP 5.6.11 and my web host runs 5.3.29

  • 写回答

1条回答 默认 最新

  • dti70601 2016-02-17 14:46
    关注

    As @chris85 alluded to, if you change to:

    $temp->execute(array("theusername"));
    

    this may cure the problem.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 R语言卸载之后无法重装,显示电脑存在下载某些较大二进制文件行为,怎么办
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?
  • ¥15 关于#vue.js#的问题:修改用户信息功能图片无法回显,数据库中只存了一张图片(相关搜索:字符串)
  • ¥15 texstudio的问题,