Get WordPress to Read a Custom Database

(Scroll down for question if you don’t require background)

For days now I’ve been working on a project that in simple terms allows users to search an internal company ID and then be shown a load of information relating to that ID (all stored in a SQL db). Just to give you a tiny bit of background, I’ve always worked with WordPress, I love WP – Custom post types, custom fields, etc – I’ve always found it to be a good solution for most things.

Read More

Now I’m at something of a roadblock. This database that I need a “front-end” to read cannot be changed, it also cannot be moved (/merged with WP tables, etc) as other apps read the same “live” data from it.

I have managed to put together a tiny (sort-of) front end and search function. But I’m still coming across loads of issues, I’m not a pro at PHP/SQL, etc.

My question is:

For me, WordPress seems to to be the best option. How do I get WordPress to “read” from a database entirely separate from WP and “generate” pages based on this data (so that users can search, see info in this custom data, etc), like custom post types/fields but from a db that was never generated via WP?
I understand that a WP table would still be required (but separate) to run the core functions of WP, etc.

Any help would be greatly appreciated!

Related posts

Leave a Reply

4 comments

  1. I think WP is not the best options in this case. Why you don’t create a new application using some php/python framework to do that ? It looks like a simple application – just to search App/IDs and show some information.

    If you will use WP, I think it will very complicated, because you will have a lot of working making some type of your-sql-version to wp-sql-version and all…

    Try some PHP Framework as Laravel( http://laravel.com/) or even Web2py(a simple and good python framework( http://www.web2py.com/). Web2py is really simple and can connect you in your existed database.

    Hope it could be useful for you.

  2. The internals of WordPress are heavily customized for the specific databases that WordPress is designed for. Although you could probably force it to do what you want using various hooks and filters, it would be a lot of work and probably fragile.

    The most practical approach would seem to be to have WordPress “mirror” your other database.

    1) Create one or more custom post types for the type of information contained in your other database

    2) Write a synchronization program that takes data out of the other database and creates posts of the appropriate custom type with the imported data as content (and deletes custom posts that are not longer needed). You would need to have some way (e.g. post slug or custom post metadata) for recording which other DB record a given post came from so you did not create duplicates).

    The synchronization program would be quite straightforward, and when you were done you would be able to use all of the standard WordPress features with your other data.

  3. Sounds like you’re making it more complex than it needs to be. Use http://codex.wordpress.org/Class_Reference/wpdb to read/write to the database and access the tables and data you want, i.e.

    $mydb = new wpdb('username','password','database','localhost');
    $rows = $mydb->get_results("select Name from my_table");
    echo "<ul>";
    foreach ($rows as $obj) :
       echo "<li>".$obj->Name."</li>";
    endforeach;
    echo "</ul>";
    

    and use custom page templates http://codex.wordpress.org/Page_Templates if needed to display both the standard WordPress loop as well as the custom data.

    Displaying data from that database is one thing; searching is quite another. In order to search that custom data, you will need to run a search with php just on those tables, since they are not standard WordPress tables and as such aren’t searchable by the WordPress site search functions. See Creating a search form in PHP to search a database? In that case, you will need a custom page template to display the php search form and the results.

  4. WordPress use EZSQL for database connection and it says that you can create more then one connections.

    So your first database connection variable is $wpdb, You can create another with different database
    $wpdb1 = new wpdb(‘username’,’password’,’database2′,’localhost’);

    Now you can query with second database with varisble $wpdb2.

    Read more about EZSQL go to justinvincent.com/ezsql