Can one convert a MySQL connection to a PDO connection?

Tags: pdo mysql php
By : Jrgns

I work on a app which has only one way to get a working DB connection: a function that returns a MySQL connection resource.

Is there any way for me to convert that MySQL connection to a PDO MySQL connection?

I don't have access to the MySQL server in any other way, no username, password, nothing. I can't see/get the file with the function in it, either. I might be able to, later, but still, I'd like to know if it is possible.

By : Jrgns


No - that's impossible. PDO and the ext/mysql are two completely different components to access a MySQL server.

Without username, password, host (can be retrieved by using string mysql_get_host_info ([ resource $link_identifier ] ) and database name (can be retrieved by looking at a list of databases on the server with resource mysql_list_dbs ([ resource $link_identifier ] )), you won't be able to connect to the server with any other method than the given one. It's impossible to read the username and/or password from a given ext/mysql resource.

In fact, Unit Testing is the only use that I've been able to bring myself to use the InternalsVisibleToAttribute for. With this you can implement a large portion of your 'private' methods as internal instead to expose them to the unit testing framework for more invasive testing of class-internal invariants.

I've had great success with this technique. If nothing else it helps you approach that mythical 100% code coverage goal by letting you invoke private methods in situations that are otherwise inaccessible.

I think using InternalsVisibleToAttribute to enable unit testing is perfectly reasonable. My "unit" in "unit testing" is a class, and that includes internal classes, so I want to test them. I don't want to unit test private methods, though.

I don't think creating a special, private interface just for tests is a good idea. One of the values of unit testing is that it gives you a chance to think about the interface to your class from the point of view of a consumer of that class; providing a back door takes that benefit away.

My preference, however, is to put my unit tests in the same assembly as my production code. It doesn't usually affect my customer, but it does simplify things for me, so I do it. When I do, it makes the InternalsVisibleTo question go away.

This video can help you solving your question :)
By: admin