Mixed mysqli and PDO

Hello everyone!

I’m trying to put my database online put in my code i have PHP files that are only PHP and others PDO .

My Question is : Is there anyway I can mix these too when linking the files in database.php

<?php
   $user='';
   $pass='';
   $db='';
  $dbserver='';

//Regular PHP
 $conn=new mysqli($dbserver, $user,$pass,$db) or die("error");
    if ($conn->connect_error) {
      die("Connection failed: " . $conn->connect_error);
     } 
//PDO
try {
$conn = new PDO("mysql:host=$dbserver;dbname=$db", $user, $pass);
// set the PDO error mode to exception
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
echo "Connected successfully"; 
} catch(PDOException $e) {    
echo "Connection failed: " . $e->getMessage();
}

?>

If there is a way, I don’t think it’s a good idea.
It is quite possible to have one script use PDO and another one use mysqli. But I would advise to choose one and stick to it.
In the code you posted the pdo $conn will overwrite the mysqli $conn.

Use PDO only and be done with it. And stop outputting internal errors to the user. That info is only good to hackers.

1 Like

It is highly recommended that you not do that. Mixing 2 different database APIs together will cause your application to crash or have outrageously large amounts of fatal errors or warnings. If you are trying to support both database APIs, there is a better way by separating the two into 2 different files that run equivalent functions.

1 Like

Use PDO only. It’s a better idea not to mix these together.

If anyone happens to be looking for a textbook example of Fear Uncertainty Doubt (FUD) then link to the above quote. As a bonus, it is also 100% fact free.

It depends how you go about the mixing. If you have a mysqli resource and try PDO methods or vice versus, it will error. If you use both but keep the resources and their associated methods in order, you may not get errors, but the code will be prone to confusion that might result in errors.

For example, it would be possible to use mysqli for a SELECT and PDO for an INSERT, but why? Two resource connections might not be disastrous but seems totally pointless to do to me.

The only exception where I’d want both is if I were refactoring code a bit at a time as I had the time to modify code I didn’t have the time to change all at once.

Would you agree that mixing them is a bad idea, something best avoided?

Sure. But there are times you might want to include a 3rd party module which might happen to use a different database api. I was simply reacting to the amazingly silly assertion in the quote.

1 Like

I don’t think it’s silly at all. We had someone mix matching PDO and mysqli_ earlier last year. When I went to debug it, it came up with like 4 or 5 different errors. I honestly don’t think it’s “FUD” at all. If you want to, go right a head. But I won’t debug someone’s errors if they’ve been told more than once to not mix match database APIs. Now, that’s more silly.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.