• Resolved massa974

    (@massa974)


    Hi guys,

    So here’s the thing : I was recently hired as a WordPress trainer for students who have never used it. I accepted the job because the training program felt entirely under my scope of skills.

    But when I met the students, I learned that there’s a mandatory part of the training that I have no idea how to deal with :

    On a WordPress based subdomain, they need to connect to an external database which is linked to a Joomla install, to gather and display data from this external database.

    Again, this is way out of my scope of skills, so I’d definately like some insights from you guys. (detailed if possible)

    Please forgive me if this sounds like I want you to make my job, but since my employer here seems to see my incapacity in this field in an infavourable light, some help would be awesome.

    Thanks !

Viewing 7 replies - 1 through 7 (of 7 total)
  • It’s pretty easy to do. Something like this…

    $my_wpdb = new WPDB( 'remote_user', 'remote_password', 'remote_db', 'remote_server');

    Then you can use this new object just like the main $wpdb object and query pretty much anything that you want to.

    Thread Starter massa974

    (@massa974)

    Thank you VERY much for your answer.

    Actually I read somewhere that this was the way to go, but where do I paste this code?

    And sorry for asking, but when I paste that code where it need be, how exactly do I get to display the data from the external database? (Informations about various training offers)

    Sorry if this sounds like stupid questions and thanks again.

    but where do I paste this code?

    Where ever it needs to go in your system. That could be in any one of 100’s (or 1,000’s) of files depending on how your site is set up. Most times it woudl be done in a contral place liek your themes functions.php file, or in a plugins main PHP file, but that might not suit the purposes of what you need to do with it.

    how exactly do I get to display the data from the external database?

    Look at the WPDB class reference and it will show you exactly what can be done.

    Thread Starter massa974

    (@massa974)

    Thanks for pointing me to this resource.

    There’s all I need indeed, but I’m wondering :

    About that code :

    $my_wpdb = new WPDB( 'remote_user', 'remote_password', 'remote_db', 'remote_server');

    I just add it at the end of functions.php right ?
    I shall also replace remote_user with username ? Same goes for remote_password and remote_db I guess.
    But what about remote_server : what should I replace this with ? The IP of the server ?

    Plus, where exactly do I need to need to paste the code to display the content of external database ? (I need the data displayed on a specific WP page)

    Thanks again

    EDIT : pasting the given code in functions.php won’t replace the database of the current install ?

    I just add it at the end of functions.php right ?

    Maybe. Maybe not. As I said before, it all depends on where you’re going to need to use it. It could go in any of 1,000 different places. A theme, plugins, shortcode, widget, etc… While you can set it up in one central spot, you need to have an idea of where it’s going to be used before you can say where the code should actually go.

    But what about remote_server : what should I replace this with ? The IP of the server ?

    You would replace that with whatever your hosting company/server technicians tell you is the host name of the server that you’re connecting to.

    Plus, where exactly do I need to need to paste the code to display the content of external database ? (I need the data displayed on a specific WP page)

    I’d suggest looking into shortcodes. That will enable you to output whatever data you want at any point in a pages/posts content.

    pasting the given code in functions.php won’t replace the database of the current install ?

    No, as long as you don’t call the new database connection $wpdb the standard connection will remain untouched.

    Thread Starter massa974

    (@massa974)

    Just to follow up on this :

    it worked !

    Thanks for helping me keeping my job ??

    can you post your code massa974? would love to know how you showed an external database in WP

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Connect to an external database’ is closed to new replies.