Title: Connecting second database to WordPress
Last modified: July 27, 2017

---

# Connecting second database to WordPress

 *  [buffyfan](https://wordpress.org/support/users/buffyfan/)
 * (@buffyfan)
 * [8 years, 9 months ago](https://wordpress.org/support/topic/connecting-second-database-to-wordpress/)
 * I have two ecommerce websites. They sell the same products at the same prices.
   One is WordPress (woocommerce). The other has a .NET backend and a MSSQL database.
   I don’t want to have to update both databases. I am very new at dealing with 
   databases. Is there a way to link my WordPress products (their images, price,
   stock level, etc) to the MSSQL database?

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

 *  [catacaustic](https://wordpress.org/support/users/catacaustic/)
 * (@catacaustic)
 * [8 years, 9 months ago](https://wordpress.org/support/topic/connecting-second-database-to-wordpress/#post-9359347)
 * There is… but it’s not pretty.
 * Depending on what the second sote built on .Net is, there may be a “gateway” 
   or adapter that can work between that and WordPress, but the chances of that 
   are pretty slim. Still worth a look though.
 * If there’s nothing out there. you’ll have to write your own bridge between the
   two systems. It’s won’t be easy, but it is possible. You’ll need to know all 
   about the database schemas on both sides, and how they will corresond to the 
   other side, and then set up triggers in both systems that will be activated when
   changes are made that push the changes through to the other system.
 *  Thread Starter [buffyfan](https://wordpress.org/support/users/buffyfan/)
 * (@buffyfan)
 * [8 years, 9 months ago](https://wordpress.org/support/topic/connecting-second-database-to-wordpress/#post-9393935)
 * Thanks for the info [@catacaustic](https://wordpress.org/support/users/catacaustic/)
   
   I am able to update a CSV file, no problem, that will list stock levels. Then
   I need to set up a WordPress Cron job to read the CSV file every hour and update
   the database…unfortunately I’ve never written a cron job before. Can i define
   it in functions.php? If you have any advice on writing a cronjob for reading 
   a CSV file and updating the database with it, i’d love to hear it as I haven’t
   worked with WordPress before!
 * this is as far as I have gotten:
 * // Scheduled Action Hook
    function updatedb( ) { }
 * // Schedule Cron Job Event
    function update_stock_cron_job() { if ( ! wp_next_scheduled(‘
   updatedb’ ) ) { wp_schedule_event( time(), ‘hourly’, ‘updatedb’ ); } } add_action(‘
   wp’, ‘update_stock_cron_job’ );
    -  This reply was modified 8 years, 9 months ago by [buffyfan](https://wordpress.org/support/users/buffyfan/).
 *  Thread Starter [buffyfan](https://wordpress.org/support/users/buffyfan/)
 * (@buffyfan)
 * [8 years, 8 months ago](https://wordpress.org/support/topic/connecting-second-database-to-wordpress/#post-9420669)
 * I tried this but it doesn’t work and stopped people being able to add anything
   to their basket.
 *     ```
       <?php
       // Scheduled Action Hook
       function updatedb( ) {
       $myfile = fopen("somefilename.csv", "r") or die("Unable to open file!");
       $csvArray = array_map('str_getcsv', file($myfile));
       global $wpdb;
   
       foreach($csvArray as $csvEntry) {
       $value = $csvEntry[2];
       $field = $csvEntry[1];
       $sku = $csvEntry[0];
       $wpdb->update(
               ppwp_postmeta,
               array(  meta_value = $value,
                      ),
               array(  meta_key = $field AND
                     post_id = (select post_id
                      from (select
                        meta_value,
                         post_id
                         from ppwp_postmeta
                         where meta_key = '_sku') as sku_post
                         where meta_value = $sku);
                      )
               );
       }
   
       fclose($myfile);
       }
       // Schedule Cron Job Event
       function update_stock_cron_job() {
       if ( ! wp_next_scheduled( ‘updatedb’ ) ) {
       wp_schedule_event( time(), ‘hourly’, ‘updatedb’ );
       }
       }
       add_action( ‘wp’, ‘update_stock_cron_job’ );
       ?> 
       ```
   
 * Any help GREATLY appreciated.
    -  This reply was modified 8 years, 8 months ago by [buffyfan](https://wordpress.org/support/users/buffyfan/).
 *  [catacaustic](https://wordpress.org/support/users/catacaustic/)
 * (@catacaustic)
 * [8 years, 8 months ago](https://wordpress.org/support/topic/connecting-second-database-to-wordpress/#post-9422667)
 * So what errors are you seeing? What’s happening through the code? What de-bugging
   have you done?
 * As bad as it is, “not working” doesn’t give anyone much to go on, as it could
   be one of 1,000’s of things from code problems to server configuration.

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

The topic ‘Connecting second database to WordPress’ is closed to new replies.

## Tags

 * [database](https://wordpress.org/support/topic-tag/database/)

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 4 replies
 * 2 participants
 * Last reply from: [catacaustic](https://wordpress.org/support/users/catacaustic/)
 * Last activity: [8 years, 8 months ago](https://wordpress.org/support/topic/connecting-second-database-to-wordpress/#post-9422667)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
