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 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥15 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化