Greetings, I need create and article/page where i will

Greetings,

I need create and article/page where i will display a list of data.
I need to fecth that data from another database on the same host.

I've tried several things, but with no sucess so far.

Is it possible to connect to another database inside a post? Any tutorial or tips on this please?

Best Regads,
Diogo Gomes

  • Alexander
    • DEV MAN’s Mascot

    Hi @diugo,

    What have you tried so far?

    This would require some custom coding but could be possible by registering a shortcode, or by adding a filter hook the 'the_body'

    Then with the function you've attached to the shortcode or filter, you could perform your database queries. It's probably best to just register a new shortcode. It's basically a way to call any function you've made within a post or article. Here's a link with code references for this: http://codex.wordpress.org/Shortcode_API

    Curious to see what you've already tried, or if there are any specific hangups you're running into. I'd love to help you figure this out!

    Thanks for being a WPMU DEV member.
    Best regards,

  • diugo
    • Design Lord, Child of Thor

    Hi Alexander,

    Thanks for your reply.
    I've tried connecting with PDO, mysql connect, the wpdb class...
    But nothing is retrieve.

    I thought that maybe wordpress filters were blocking my functions.

    Going to try first to connect with an php file outside the wordpress, to test the code.
    I should have done that first.

    Thanks for you help,
    Diogo Gomes

  • Alexander
    • DEV MAN’s Mascot

    Hey again @diugo, here's a suggestion for you.

    This would require the data to be in the same database (not seperate as you've mentioned) but can be very useful, so I figured I'd at least mention it.

    One nice thing about Wordpress is that it maintains a database connection for you. There is a Wordpress Database object that is already instantiated and ready to work with wherever you need. You can find more info on this here:

    http://codex.wordpress.org/Class_Reference/wpdb

    To use it, you just do something like this:

    global $wpdb;
    
    $rows =  $wpdb->get_results( "SELECT * FROM wp_your_table" );

    Hope this helps!

    Best regards

  • pianoman
    • Site Builder, Child of Zeus

    A couple of things you need to check on the DB front. Some hosting providers block external access to DBs and even access to DB's on different domains on the same host.

    If you are using a cPanel, see if you have an option for "Remote Database Access Hosts", you made need to create entries for the i.p.'s domains accessing the database.

    Also, note that some hosting providers do NOT install databases locally even for shared hosting, a good example is 1and1 hosting. Often this can be forgotten if one is used to installing WordPress with a "canned" installation using Fantastico or similar tool. Double check your wp-config.php for your DB entries.

    Don't forget to check the Port number. Many shared hosting providers, change the default port for MySQL to reduce robot based attacks.

    I forgot to mention you can wildcard "%" the database access for testing purposes. Do NOT forget to undo any wildcard. If not, you are opening yourself to a denial of service attack on your database port!

  • diugo
    • Design Lord, Child of Thor

    Hi,

    Thanks for all the replies.

    I don't want to duplicate the tables from one domain to another domain.
    We have a dedicated host.

    Can you please just confirm me if it's possible to connect to an external database within an wordpress page?
    I just need to fetch the data and display it on the page, I don't need do manipulate the data.

    Best Regards,
    Diogo Gomes

  • Alexander
    • DEV MAN’s Mascot

    Gotcha, so a separate db is 100% required here. Yes, you can use the wordpress database class, but you would of course need to instantiate a new connection. You wouldn't be able to piggy back in on the $wpdb instance.

    I think @pianoman has all the needed info under control here. Great article! The last line in there is what's most important, where you're creating your own instance.

    Best regards,

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.