PHP Pdo VS Normal MySQL_Connect

PHP PDO vs normal mysql_connect

PDO is a bit slower than the mysql_*
But it has great portability. PDO provides single interface across multiple databases. That means you can use multiple DB without using mysql_query for mysql, mssql_query for MS sql etc. Just use something like $db->query("INSERT INTO...") always. No matter what DB driver you are using.

So, for larger or portable project PDO is preferable. Even zend framework use PDO.

Which is better: mysql_connect or mysql_pconnect

If you are going to write a web page there is no need of persistent connection. It takes too much resources. Use mysql_connect. Minimize the time your db connection is open and not used as much as you can. Open, fetch what you want, close. It doesn't need to stay open while the users are just reading. The connection will be used eventually if they respond - INSERT/go to another page..

Here are some good points about NOT USING persistent connection in web applications

  • When you lock a table, normally it is unlocked when the connection closes, but since persistent connections do not close, any tables you
    accidentally leave locked will remain locked, and the only way to
    unlock them is to wait for the connection to timeout or kill the
    process. The same locking problem occurs with transactions. (See
    comments below on 23-Apr-2002 & 12-Jul-2003)

  • Normally temporary tables are dropped when the connection closes, but since persistent connections do not close, temporary tables aren't
    so temporary. If you do not explicitly drop temporary tables when you
    are done, that table will already exist for a new client reusing the
    same connection. The same problem occurs with setting session
    variables. (See comments below on 19-Nov-2004 & 07-Aug-2006)

  • If PHP and MySQL are on the same server or local network, the connection time may be negligible, in which case there is no advantage
    to persistent connections.

  • Apache does not work well with persistent connections. When it receives a request from a new client, instead of using one of the
    available children which already has a persistent connection open, it
    tends to spawn a new child, which must then open a new database
    connection. This causes excess processes which are just sleeping,
    wasting resources, and causing errors when you reach your maximum
    connections, plus it defeats any benefit of persistent connections.
    (See comments below on 03-Feb-2004, and the footnote at
    http://devzone.zend.com/node/view/id/686#fn1)

how to change mysql_connect() to PDO

class DB {
function DB() {
$this->host = "localhost";
$this->db = "dbtest";
$this->user = "root" ;
$this->pass = "password";
$this->link = new PDO("mysql:host=$this->host;dbname=$this->db", $this->user, $this->pass);
}
function query($query) {
$result = $this->link->query($query);
return $result;
}
function thislink() {
return $this->link;
}
function close() {
$this->link = NULL;
}
}

Newsletter works with mysql_connect but not with PDO

In the pdo side, you need to fetch the query as well. Do this:

$pdo->query($sql)->fetchAll()

PHP: Reuse Open MySQL Connection from mysql_connect() PDO?

I am just curious to see if there is a way to transfer a connection opened by mysql_connect() to PDO.

NO

I would like for the class's queries to work regardless on the user opening a connection to the database via mysql_* or PDO...

It's quite hard to understand your reasons.

If you want to keep both drivers, you have to keep 2 sets of functions as well. And completely change the class structure. You have to have an abstract class with common methods and 2 implementations for them. No connections passed to the class but class itself have to manage connection. I doubt it worth such a labor though.

If you just want to allow use of your class along with some legacy mysql_* code - just open another connection from PDO. It's not such an evil thing to be avoided at any cost. It's just a connection. It will burden your server slightly, yes, but it will be least problem for the application that uses such a sandwiched spaghetti.



Related Topics



Leave a reply



Submit