Just some guy
Just some guy

Reputation: 1959

PHP PDO - Using prepared statements causes server error

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

Upvotes: 1

Views: 305

Answers (1)

rybo111
rybo111

Reputation: 12598

As @chris85 alluded to, if you change to:

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

this may cure the problem.

Upvotes: 4

Related Questions