Title: Retrieving data from an external mysql database
Last modified: August 19, 2016

---

# Retrieving data from an external mysql database

 *  [mayiek](https://wordpress.org/support/users/mayiek/)
 * (@mayiek)
 * [15 years, 8 months ago](https://wordpress.org/support/topic/retrieving-data-from-an-external-mysql-database/)
 * 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)

 *  [MichaelH](https://wordpress.org/support/users/michaelh/)
 * (@michaelh)
 * [15 years, 8 months ago](https://wordpress.org/support/topic/retrieving-data-from-an-external-mysql-database/#post-1734532)
 * If you put those tables in the same database as your other WordPress tables you
   can access those tables with [wpdb](http://codex.wordpress.org/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](http://wordpress.org/support/topic/pros-and-cons-of-adding-tables-to-the-wp-db?replies=8#post-1201613)
 *  Thread Starter [mayiek](https://wordpress.org/support/users/mayiek/)
 * (@mayiek)
 * [15 years, 8 months ago](https://wordpress.org/support/topic/retrieving-data-from-an-external-mysql-database/#post-1734581)
 * 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
 *  [MichaelH](https://wordpress.org/support/users/michaelh/)
 * (@michaelh)
 * [15 years, 8 months ago](https://wordpress.org/support/topic/retrieving-data-from-an-external-mysql-database/#post-1734603)
 * 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](https://wordpress.org/support/users/mayiek/)
 * (@mayiek)
 * [15 years, 8 months ago](https://wordpress.org/support/topic/retrieving-data-from-an-external-mysql-database/#post-1734616)
 * 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](https://wordpress.org/support/users/mayiek/)
 * (@mayiek)
 * [15 years, 8 months ago](https://wordpress.org/support/topic/retrieving-data-from-an-external-mysql-database/#post-1734638)
 * `$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)…
 *  [MichaelH](https://wordpress.org/support/users/michaelh/)
 * (@michaelh)
 * [15 years, 8 months ago](https://wordpress.org/support/topic/retrieving-data-from-an-external-mysql-database/#post-1734688)
 * You will need to get conversant with [The Loop](http://codex.wordpress.org/The_Loop)
   use a post variable such as `$post->post_title`
 *  Thread Starter [mayiek](https://wordpress.org/support/users/mayiek/)
 * (@mayiek)
 * [15 years, 8 months ago](https://wordpress.org/support/topic/retrieving-data-from-an-external-mysql-database/#post-1734730)
 * 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](https://wordpress.org/support/users/mayiek/)
 * (@mayiek)
 * [15 years, 8 months ago](https://wordpress.org/support/topic/retrieving-data-from-an-external-mysql-database/#post-1734734)
 * 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?
 *  [MichaelH](https://wordpress.org/support/users/michaelh/)
 * (@michaelh)
 * [15 years, 8 months ago](https://wordpress.org/support/topic/retrieving-data-from-an-external-mysql-database/#post-1734747)
 * I’d consider putting that in a template that displays the posts or at the very
   least use a [Shortcode](http://codex.wordpress.org/Shortcode_API).
 * Related:
    [Stepping Into Template Tags](http://codex.wordpress.org/Stepping_Into_Template_Tags)
   [Stepping Into Templates](http://codex.wordpress.org/Stepping_Into_Templates)
   [Template Hierarchy](http://codex.wordpress.org/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.

 * 9 replies
 * 2 participants
 * Last reply from: [MichaelH](https://wordpress.org/support/users/michaelh/)
 * Last activity: [15 years, 8 months ago](https://wordpress.org/support/topic/retrieving-data-from-an-external-mysql-database/#post-1734747)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
