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

图片转代码服务由CSDN问答提供 功能建议

我有一个PHP网站,我花了一些时间使用XAMPP localhost环境进行开发。 当我在本地测试它时,它工作得很好但不幸的是,当我尝试在我租用的Web服务器空间上运行它时,它是不可能的。

当我上传完成的网站时 并尝试访问它我遇到了'服务器错误500'和经过几个小时的狩猎挫折后,我已经确定了导致错误的原因。 原来,执行预处理语句会导致服务器错误,我不明白为什么。 以下代码将导致服务器错误:

  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  - >执行([ “theusername”]);  //导致错误的是这一行。
   
 
 

以下代码执行相同的操作但没有准备好的语句; 它对Web主机没有任何问题:

  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'“); \  n   
 
 

当使用XAMPP在本地运行时,它们都能正常工作。 如果我尝试使用预准备语句,任何人都知道为什么Web主机会给我服务器错误?

编辑:

我的XAMPP安装运行PHP 5.6 .11和我的网络主机运行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.

    已采纳该答案
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题