• I am trying to retrieve data from an external database into WP posts. I have created a site – almost a list – that posts the data of some kind of shops in each one’s post. So far I follow a totally foolish way; I manual enter all their data in each post : telephone, email, address, etc.

    I have created an external mysql database with all the fields and I do not know how to read them. I will have a template for all posts :
    for example:
    Address: SELECT shopAddress FROM shops_table WHERE shop_name=post_title;
    or something like that.

    I will be glad If anyone could assist me a little…

    thanks in advance

Viewing 9 replies - 1 through 9 (of 9 total)
  • If you put those tables in the same database as your other WordPress tables you can access those tables with wpdb.

    But consider some of these options:
    http://wordpress.org/support/topic/pros-and-cons-of-adding-tables-to-the-wp-db?replies=8#post-1201613

    Thread Starter mayiek

    (@mayiek)

    Hi Michael,

    I have already read that post and I was getting this error:

    Warning: Invalid argument supplied for foreach() in /hermes/bosweb/web272/b2727/website_here/wordpress/wp-content/plugins/exec-php/includes/runtime.php(42) : eval()’d code on line 4

    I pasted this code in a post (using php-exec):

    <?php $wpdbtest_otherdb = new wpdb('username', 'password', 'shops', 'websitesql.whatever.com');
    $wpdbtest_otherdb->show_errors();
    $myshops = $wpdbtest_otherdb->get_results("SELECT * FROM shops");
    foreach ($myshops as $myshop) {echo $myshop->shopName, '<br />';
    }  ?>

    and that’s when I decided to start a new thread πŸ™‚

    the reason for the SELECT * quote is just to test if this works. I know much about c++ programming but I’m a newbie in php so I don’t know 100% of what happens in the code.

    Thanks for your reply

    Could be that comma in the echo statement.

    Try

    <?php
    $wpdbtest_otherdb = new wpdb('username', 'password', 'shops', 'websitesql.whatever.com');
    $wpdbtest_otherdb->show_errors();
    $myshops = $wpdbtest_otherdb->get_results("SELECT * FROM shops");
    foreach ($myshops as $myshop) {
    echo $myshop->shopName . '<br />';
    }
    ?>

    Thread Starter mayiek

    (@mayiek)

    I am very happy to announce that it worked ! I am amazed !
    A huge “thank you” to Michael !
    Finally I got the one with the external db to work… no need to use the same db with wordpress. Now the thing is, how do I map the post title (name of the shop in wp) to the database variable (name of the shop in sql db) to start retrieving data.

    the function for the retrieving of data must be used about 30 times. What should I use?

    $myshops = $wpdbtest_otherdb->get_results("SELECT * FROM shops");
    foreach ($myshops as $myshop) {
    echo $myshop->shopName . '<br />';

    where “echo $myshop->VariableFromTable . ‘
    ‘;” ??

    Thanks

    Thread Starter mayiek

    (@mayiek)

    $myshops = $wpdbtest_otherdb->get_results("SELECT * FROM shops WHERE shopName=wp_title()");

    I suppose I cannot use something like that… I get a message : Fatal error: Can’t use function return value in write context in…..

    How can I pass the return value of wp_title() to the sql quote? The value of the shopName is an varchar(50)…

    You will need to get conversant with The Loop use a post variable such as $post->post_title

    Thread Starter mayiek

    (@mayiek)

    As I know nothing about The Loop, I read that single_post_title(); works out of it. I tried that:

    $quoteName=single_post_title();
    $myshops = $wpdbtest_otherdb->get_results("SELECT * FROM shops WHERE shopName='$quoteName'");
    foreach ($myshops as $myshop) {echo $myshop->shopName,' ', $myshop->shopEmail;}
     ?> </p>

    but I am getting an empty page πŸ™‚

    I have tried get_the_title(); , the_title();, single_post_title();, $post->post_title, but none of them seems to work. I believe this is a variable to variable mapping issue.

    Thread Starter mayiek

    (@mayiek)

    Finally I made it.

    Just to make everyone know (who might be searching for the same thing):

    $quoteName=single_post_title('',false);
    $myshops = $wpdbtest_otherdb->get_results("SELECT * FROM shops WHERE shopName='$quoteName'");
    foreach ($myshops as $myshop) {echo $myshop->shopName,' ', $myshop->shopEmail;}
     ?> </p>

    The whole thing was that $quoteName=single_post_title('',false);

    I am very very happy πŸ™‚

    Do I need to enter `$wpdbtest_otherdb = new wpdb(‘username’, ‘password’, ‘shops’, ‘websitesql.whatever.com’);
    $wpdbtest_otherdb->show_errors();`
    in every post’s html code, or could I do it only once for all posts?

    I’d consider putting that in a template that displays the posts or at the very least use a Shortcode.

    Related:
    Stepping Into Template Tags
    Stepping Into Templates
    Template Hierarchy

Viewing 9 replies - 1 through 9 (of 9 total)

The topic ‘Retrieving data from an external mysql database’ is closed to new replies.