0

I'm using PDO in a website, now I need a php class that uses mysql_xxx functions. It doesn't connect and select db itself, you should use it like this:

    if (!($connection = @mysql_connect($MySQL_host, $MySQL_user, $MySQL_pass))) {
        die('Error connecting to the database!');        }
    if (!@mysql_select_db($MySQL_database, $connection)) {
        die('Error selecting database!');                }

    require '../Zebra_Mptt.php';
    $mptt = new Zebra_Mptt();
    $foo = $mptt->add(0, 'Foo');

Can I create and share a connection between PDO and mysql_ functions or anything more efficient?

Positivity
  • 5,406
  • 6
  • 41
  • 61
  • What does the connection above have to do with PDO at all? Where is the connection? – arkascha Aug 21 '13 at 06:47
  • @arkascha I myself use PDO. here I wrote just the code needed for making this class work. – Positivity Aug 21 '13 at 06:51
  • Still I don't udnerstand what the connection is. If you use PDO: fine. If you use the old, and depreciated mysql extension: fine. Where is the connection? _Why_ do you "have to" use both? – arkascha Aug 21 '13 at 06:52
  • @arkascha "I" use PDO, the "class developer" which I have to use his class used old, and depreciated mysql_xxx. – Positivity Aug 21 '13 at 06:58
  • @Webinan: It's free software, you are allowed to change that. See as well my answer, it's not that complicated btw. And hint hint: If you don't have the mysql_ extension enabled, just write some functions that do that with some global PDO object. hint hint. – hakre Aug 21 '13 at 06:58
  • @hakre It's enabled I just wanted to connect once. – Positivity Aug 21 '13 at 07:03
  • Ah, so I _guess_ that the class `Zebra_Mptt` internally assumes an old-style `mysql connection`, probably in form of a connection handler? That would ahve been a helpful detail in the question... – arkascha Aug 21 '13 at 07:25

2 Answers2

1

You can't. You have to create two separated connections for that. But you can also change that class to use PDO or find another one.

Elon Than
  • 9,603
  • 4
  • 27
  • 37
0

Can I create and share a connection between PDO and mysql_ functions

No, such a feature is not available with PHP.

or anything more efficient?

Sure. The efficient way is to program against interfaces. The Zebra_Mptt library should define an interface for the database adapter it uses so that you can replace it with one that is re-using your database connection.

As it currently doesn't, you would need to refactor it, encapsulate the database interaction calls into the standard database connection object for that library, then extract the interface of that connection object and then create your PDO variant and inject it instead.

I just took a brief look, the class is rather straight forward so this should be merely straight forward. All it does is firering some SQL queries and in the very beginning checking if the database is online which you can easily map and defer.

In case you don't want to do that, you can take a pre-existing wrapper for the mysql_* interface, either replace the prefix in the existing library or just disable the mysql extension and then let it create the functions based on PDO. Such a wrapper is here:

Usage:

<?php
require('/path/to/mysqlpdo.php');

mysqlpdo::createfunctions('mysql');

It will need an additional mysql_ping() and mysql_real_escape_string() wrapped to have it work though, but this should be easily feasibile, but nothing is perfect in that field.

Community
  • 1
  • 1
hakre
  • 193,403
  • 52
  • 435
  • 836
  • should I be concerned at all? – Positivity Aug 21 '13 at 07:04
  • 1
    Well concerned about what? I also showed another alternative how that can be done, but it's akward monkey patching. I would go with extracting the interface of some database connector for that class and inject it via dependency injection to the constructor. – hakre Aug 21 '13 at 07:08
  • Well you should first of all concerned about the fact that this library uses the outdated mysql_* interface and it doesn't offer any way to replace that by design. Because that is *your* issue you have with that library. Always look from your point of view to formulate concerns. – hakre Aug 21 '13 at 07:15