Mixing PDO and MySQL Functions?

我与影子孤独终老i 提交于 2020-01-15 06:18:28

问题


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..


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/12551891/mixing-pdo-and-mysql-functions

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!