Am I able to use MySQL and PDO statement together on my website?
The reason being is that my website is currently using MySQL statement and it's quite a huge website and it will take me about a month to convert everything to PDO.
I would like to implement PDO slowly page by page as my website is still running.
Here are some code samples on my site now:
On page db.php
mysql_connect ( "localhost", "test", "test" ) or trigger_error ( mysql_error(),E_USER_ERROR );
mysql_select_db ( "test_db" );
mysql_query('set names utf8');
On other pages (every other pages is linked to that db.php file):
require_once('db.php');
$sql = "SELECT * FROM test_table WHERE id='$user_id' AND status='active'";
$row = mysql_query($sql) or die(mysql_error());
$row = mysql_fetch_array($row);
So, I was thinking inside the db.php file, can I add something like this below the old mysql connection?
$servername = "localhost";
$username = "username";
$password = "password";
try {
$conn = new PDO("mysql:host=$servername;dbname=myDB", $username, $password);
// 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();
And then on other pages, I will slowly update the old mysql to PDO like below:
$stmt = $conn->prepare('SELECT * FROM employees WHERE name = :name');
$stmt->execute(array('name' => $name));
foreach ($stmt as $row) {
// do something with $row
}
Is this even possible? Because I need to update while the site is still running. Also, I might add more pages to the website also, so that I don't keep adding more pages with the old mysql connection string. The more pages I add, the tougher I need to change everything to PDO.