What does Apache need to support both mysqli and PDO?
- by Nathan Long
I'm considering changing some PHP code to use PDO for database access instead of mysqli (because the PDO syntax makes more sense to me and is database-agnostic). To do that, I'd need both methods to work while I'm making the changeover.
My problem is this: so far, either one or the other method will crash Apache.
Right now I'm using XAMPP in Windows XP, and PHP Version 5.2.8. Mysqli works fine, and so does this:
$dbc = new PDO("mysql:host=$hostname;dbname=$dbname", $username, $password);
echo 'Connected to database';
$sql = "SELECT * FROM `employee`";
But this line makes Apache crash:
$dbc->query($sql);
I don't want to redo my entire Apache or XAMPP installation, but I'd like for PDO to work. So I tried updating libmysql.dll from here, as oddvibes recommended here. That made my simple PDO query work, but then mysqli queries crashed Apache.
(I also tried the suggestion after that one, to update php_pdo_mysql.dll and php_pdo.dll, to no effect.)
Test Case
I created this test script to compare PDO vs mysqli. With the old copy of libmysql.dll, it crashes if $use_pdo is true and doesn't if it's false. With the new copy of libmysql.dll, it's the opposite.
if ($use_pdo){
$dbc = new PDO("mysql:host=$hostname;dbname=$dbname", $username, $password);
echo 'Connected to database<br />';
$sql = "SELECT * FROM `employee`";
$dbc->query($sql);
foreach ($dbc->query($sql) as $row){
echo $row['firstname'] . ' ' . $row['lastname'] . "<br>\n";
}
}
else {
$dbc = @mysqli_connect($hostname, $username, $password, $dbname) OR die('Could not connect to MySQL: ' . mysqli_connect_error());
$sql = "SELECT * FROM `employee`";
$result = @mysqli_query($dbc, $sql) or die(mysqli_error($dbc));
while ($row = mysqli_fetch_array($result,MYSQLI_ASSOC)) {
echo $row['firstname'] . ' ' . $row['lastname'] . "<br>\n";
}
}
What does Apache need in order to support both methods of database query?