2

On the existing system its use old MySQL functions.. I would like to replace it all to PDO but it would take a long time and a lot of testing.

Is it possible to mix PDO and MySQL Functions on the existing system? For example new pages/php files will use PDO... All the old files will still use old MySQL for time being and will be replaced slowly as system will continue to update..

j0k
  • 22,600
  • 28
  • 79
  • 90
I'll-Be-Back
  • 10,530
  • 37
  • 110
  • 213

2 Answers2

3

Yes, it is perfectly fine to use both at the same time. Keep in mind, though, that you would need a seperate connection for either type and beware of the implications that arise from such an approach.

Community
  • 1
  • 1
aefxx
  • 24,835
  • 6
  • 45
  • 55
1

I am new to SO but I have faced this problem too. Many people find it troublesome to switch from mysql_* to PDO.
In my case I was using a separate connection.php that stored functions to connect to database and returned the handle

function connect() 
{ 
    $cn = mysql_connect("localhost","username","pass" );
    $select = mysql_select_db("dbname", $cn);
    return($cn);
}

//For PDO
function pdoconnect()
{
    $db="";
    try
    {
        $db=new PDO("mysql:host=localhost;dbname=dbname", "username", "pass");
        $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    }
    catch (PDOException $er)
    {
        print("errr".$er."<br />");
        return(1);
    }
    return($db);
} 

while for old functions I used $cn=connect();.
I switched to $cn=pdoconnect(); for new ones.

This worked because I was already using connect.php in old files as well.

Nikola K.
  • 7,093
  • 13
  • 31
  • 39
geekman
  • 2,224
  • 13
  • 17
  • When you replaced `$cn=connect();` I switched to `$cn=pdoconnect();` - you still have to replace `mysql_query()` to PDO `prepare()`, `execute()` functions right? – I'll-Be-Back Sep 23 '12 at 11:35
  • You clearly are a very experienced programmer, I would be more than glad to know new ways – geekman Sep 23 '12 at 11:48