0

I have been tasked to write a bunch of extensions to an existing legacy PHP app. The original app is... erm... quite bad. It's 125K lines of spaghetti code littered with raw SQL statements and using the mysql_* functions. Every day I find things that really belong on TheDailyWTF. My plan so far is to keep my new code as separate as possible from the legacy code. For database access, I plan to use Idiorm and Paris, two really nice little libraries built on top of PDO.

But how will I handle database connections? Or how does PHP handle them internally? I worry that using PDO, every page call will open two separate connections to the database. One from the legact mysql_connect() call and one from PDO. Is that true, or will PHP simply share the same connection in the background? Is there any way to make PDO and the legacy mysql_* functions use the same database connection? Does it even matter at all it I opens two connections on every page load (note: the app is not using transactions)?

I'm a little hesitant about going over those 125K lines of spaghetti code and replacing all the mysql_* functions with something else. I want to touch the original code as little as possible and not break my brain on it.

Thanks in advance for any advice.

Sander Marechal
  • 22,978
  • 13
  • 65
  • 96
  • Yes. it is true. Nothing to worry about – Your Common Sense Jan 27 '11 at 07:39
  • Exact duplicate of [Getting a PHP PDO connection from a mysql_connect()?](http://stackoverflow.com/questions/2316413/getting-a-php-pdo-connection-from-a-mysql-connect) – Dan Grossman Jan 27 '11 at 07:55
  • 1
    @Dan Grossman: I don't agree it's an exact duplicate. I also want to know it it matters that I use two connections (e.g. impact on resources and performance) or if I should start ripping and replacing in the legacy app. – Sander Marechal Jan 27 '11 at 08:09
  • I's suggest to stop worrying without a reason. When (if ever) there will be impact on resources and performance, you will have **to profile** your application. And refactor it, based on the profiling results. And only if it says that 2 connections being worst bottleneck, it would be time to touch legacy code. Quite far away from your current point – Your Common Sense Jan 27 '11 at 08:59

2 Answers2

2

You will have two connections, but it's not going to break anything. See the answer to this person asking the very same question:

Getting a PHP PDO connection from a mysql_connect()?

Community
  • 1
  • 1
Dan Grossman
  • 51,866
  • 10
  • 112
  • 101
0

Those two extensions won't talk to each other and so you will end with two connections. That would be a hell lot of work and very few would benefit IMO -- only those in your shoes, who can't convert fully.

chx
  • 11,270
  • 7
  • 55
  • 129