我有一个PHP网站,我花了一些时间使用XAMPP本地主机环境进行开发。当我在本地测试它时,它运行得很好,但不幸的是,当我尝试在租用的 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->execute(["theusername"]); //It's this line that causes the error.
下面的代码做同样的事情,但没有预准备语句;它与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'");
当使用 XAMPP 在本地运行时,它们都可以正常工作。如果我尝试使用预准备语句,任何人都知道为什么网络主机会给我服务器错误?
编辑:
我的 XAMPP 安装运行 PHP 5.6.11,我的虚拟主机运行 5.3.29
正如@chris85所提到的,如果你改为:
$temp->execute(array("theusername"));
这可能会解决问题。