{"id":393,"date":"2012-03-24T00:04:01","date_gmt":"2012-03-23T23:04:01","guid":{"rendered":"http:\/\/henrik.familiendamgaard.dk\/blog\/?p=393"},"modified":"2012-05-24T00:26:49","modified_gmt":"2012-05-23T22:26:49","slug":"connect-to-another-database-in-wordpress-using-the-wpdb-class","status":"publish","type":"post","link":"http:\/\/henrik.familiendamgaard.dk\/blog\/2012\/03\/24\/connect-to-another-database-in-wordpress-using-the-wpdb-class\/","title":{"rendered":"Connect to another database in WordPress using the wpdb class"},"content":{"rendered":"<p>In WordPress the $wpdb object can talk to any number of tables, but only within database WordPress is installed. In case you need to connect to another database, you will have to instantiate your own object from the wpdb class with the appropriate connection details:<\/p>\n<pre escaped=\"true\" lang=\"php\" line=\"1\">$mydb = new wpdb( 'username' , 'password' , 'database' , 'localhost' );<\/pre>\n<p>&nbsp;<\/p>\n<p>Once the connection is made you can access the database and its tables as you would in any WordPress database:<\/p>\n<pre escaped=\"true\" lang=\"php\" line=\"1\">$rows = $mydb-&gt;get_results(\"SELECT field1,field2 FROM myTable WHERE field3=1\");\r\nforeach ($rows as $row) {\r\n  echo $row-&gt;field1;\r\n}<\/pre>\n<p>This code can be in plugins, templates, themes and almost anywhere else inside the WordPress code.<\/p>\n<!-- AddThis Share Buttons generic via filter on the_content -->\n<!-- AddThis Related Posts generic via filter on the_content -->\n","protected":false},"excerpt":{"rendered":"<p>In WordPress the $wpdb object can talk to any number of tables, but only within database WordPress is installed. In case you need to connect to another database, you will have to instantiate your own object from the wpdb class with the appropriate connection details: $mydb = new wpdb( &#8216;username&#8217; , &#8216;password&#8217; , &#8216;database&#8217; , &hellip; <a href=\"http:\/\/henrik.familiendamgaard.dk\/blog\/2012\/03\/24\/connect-to-another-database-in-wordpress-using-the-wpdb-class\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Connect to another database in WordPress using the wpdb class<\/span><\/a><!-- AddThis Share Buttons generic via filter on wp_trim_excerpt --><br \/>\n<!-- AddThis Related Posts generic via filter on wp_trim_excerpt --><\/p>\n","protected":false},"author":4,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_mi_skip_tracking":false},"categories":[18],"tags":[22,188,43,114,55,176,113],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v19.6.1 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Connect to another database in WordPress using the wpdb class \u00bb WordPress \u00bb SYN, SYN-ACK, ACK<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"http:\/\/henrik.familiendamgaard.dk\/blog\/2012\/03\/24\/connect-to-another-database-in-wordpress-using-the-wpdb-class\/\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Henrik Damgaard\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"http:\/\/henrik.familiendamgaard.dk\/blog\/2012\/03\/24\/connect-to-another-database-in-wordpress-using-the-wpdb-class\/\",\"url\":\"http:\/\/henrik.familiendamgaard.dk\/blog\/2012\/03\/24\/connect-to-another-database-in-wordpress-using-the-wpdb-class\/\",\"name\":\"Connect to another database in WordPress using the wpdb class \u00bb WordPress \u00bb SYN, SYN-ACK, ACK\",\"isPartOf\":{\"@id\":\"https:\/\/henrik.familiendamgaard.dk\/blog\/#website\"},\"datePublished\":\"2012-03-23T23:04:01+00:00\",\"dateModified\":\"2012-05-23T22:26:49+00:00\",\"author\":{\"@id\":\"https:\/\/henrik.familiendamgaard.dk\/blog\/#\/schema\/person\/df8ef26ea249218b5e03146bc0b33945\"},\"breadcrumb\":{\"@id\":\"http:\/\/henrik.familiendamgaard.dk\/blog\/2012\/03\/24\/connect-to-another-database-in-wordpress-using-the-wpdb-class\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"http:\/\/henrik.familiendamgaard.dk\/blog\/2012\/03\/24\/connect-to-another-database-in-wordpress-using-the-wpdb-class\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"http:\/\/henrik.familiendamgaard.dk\/blog\/2012\/03\/24\/connect-to-another-database-in-wordpress-using-the-wpdb-class\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Connect to another database in WordPress using the wpdb class\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/henrik.familiendamgaard.dk\/blog\/#website\",\"url\":\"https:\/\/henrik.familiendamgaard.dk\/blog\/\",\"name\":\"SYN, SYN-ACK, ACK\",\"description\":\"Henrik Damgaard\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/henrik.familiendamgaard.dk\/blog\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/henrik.familiendamgaard.dk\/blog\/#\/schema\/person\/df8ef26ea249218b5e03146bc0b33945\",\"name\":\"Henrik Damgaard\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/henrik.familiendamgaard.dk\/blog\/#\/schema\/person\/image\/\",\"url\":\"http:\/\/henrik.familiendamgaard.dk\/blog\/wp-content\/uploads\/2014\/04\/henrik.familiendamgaard.dk-henrik.jpg\",\"contentUrl\":\"http:\/\/henrik.familiendamgaard.dk\/blog\/wp-content\/uploads\/2014\/04\/henrik.familiendamgaard.dk-henrik.jpg\",\"caption\":\"Henrik Damgaard\"},\"description\":\"Hi I\u2019m Henrik and I write this blog based on useful topics I find interesting \u2013 mostly covering some of my technical interests : System Center Operations Manager (SCOM), BizTalk, Windows Server, PowerShell, SQL server, Internet Information Services, PHP and .NET I am currently holding a position as System Administrator with focus on Microsoft server products.\",\"sameAs\":[\"https:\/\/www.google.com\/accounts\/o8\/id?id=AItOawm9nhUQo8VH_qbGDFWjC6z6lwgwvf8pZWQ\",\"http:\/\/www.facebook.com\/nrjizer\",\"http:\/\/linkedin.com\/in\/henrikdamgaard\",\"https:\/\/twitter.com\/http:\/\/twitter.com\/nrjizer\"],\"url\":\"http:\/\/henrik.familiendamgaard.dk\/blog\/author\/henrik\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Connect to another database in WordPress using the wpdb class \u00bb WordPress \u00bb SYN, SYN-ACK, ACK","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"http:\/\/henrik.familiendamgaard.dk\/blog\/2012\/03\/24\/connect-to-another-database-in-wordpress-using-the-wpdb-class\/","twitter_misc":{"Written by":"Henrik Damgaard","Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"http:\/\/henrik.familiendamgaard.dk\/blog\/2012\/03\/24\/connect-to-another-database-in-wordpress-using-the-wpdb-class\/","url":"http:\/\/henrik.familiendamgaard.dk\/blog\/2012\/03\/24\/connect-to-another-database-in-wordpress-using-the-wpdb-class\/","name":"Connect to another database in WordPress using the wpdb class \u00bb WordPress \u00bb SYN, SYN-ACK, ACK","isPartOf":{"@id":"https:\/\/henrik.familiendamgaard.dk\/blog\/#website"},"datePublished":"2012-03-23T23:04:01+00:00","dateModified":"2012-05-23T22:26:49+00:00","author":{"@id":"https:\/\/henrik.familiendamgaard.dk\/blog\/#\/schema\/person\/df8ef26ea249218b5e03146bc0b33945"},"breadcrumb":{"@id":"http:\/\/henrik.familiendamgaard.dk\/blog\/2012\/03\/24\/connect-to-another-database-in-wordpress-using-the-wpdb-class\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["http:\/\/henrik.familiendamgaard.dk\/blog\/2012\/03\/24\/connect-to-another-database-in-wordpress-using-the-wpdb-class\/"]}]},{"@type":"BreadcrumbList","@id":"http:\/\/henrik.familiendamgaard.dk\/blog\/2012\/03\/24\/connect-to-another-database-in-wordpress-using-the-wpdb-class\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Connect to another database in WordPress using the wpdb class"}]},{"@type":"WebSite","@id":"https:\/\/henrik.familiendamgaard.dk\/blog\/#website","url":"https:\/\/henrik.familiendamgaard.dk\/blog\/","name":"SYN, SYN-ACK, ACK","description":"Henrik Damgaard","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/henrik.familiendamgaard.dk\/blog\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/henrik.familiendamgaard.dk\/blog\/#\/schema\/person\/df8ef26ea249218b5e03146bc0b33945","name":"Henrik Damgaard","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/henrik.familiendamgaard.dk\/blog\/#\/schema\/person\/image\/","url":"http:\/\/henrik.familiendamgaard.dk\/blog\/wp-content\/uploads\/2014\/04\/henrik.familiendamgaard.dk-henrik.jpg","contentUrl":"http:\/\/henrik.familiendamgaard.dk\/blog\/wp-content\/uploads\/2014\/04\/henrik.familiendamgaard.dk-henrik.jpg","caption":"Henrik Damgaard"},"description":"Hi I\u2019m Henrik and I write this blog based on useful topics I find interesting \u2013 mostly covering some of my technical interests : System Center Operations Manager (SCOM), BizTalk, Windows Server, PowerShell, SQL server, Internet Information Services, PHP and .NET I am currently holding a position as System Administrator with focus on Microsoft server products.","sameAs":["https:\/\/www.google.com\/accounts\/o8\/id?id=AItOawm9nhUQo8VH_qbGDFWjC6z6lwgwvf8pZWQ","http:\/\/www.facebook.com\/nrjizer","http:\/\/linkedin.com\/in\/henrikdamgaard","https:\/\/twitter.com\/http:\/\/twitter.com\/nrjizer"],"url":"http:\/\/henrik.familiendamgaard.dk\/blog\/author\/henrik\/"}]}},"jetpack_featured_media_url":"","_links":{"self":[{"href":"http:\/\/henrik.familiendamgaard.dk\/blog\/wp-json\/wp\/v2\/posts\/393"}],"collection":[{"href":"http:\/\/henrik.familiendamgaard.dk\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/henrik.familiendamgaard.dk\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/henrik.familiendamgaard.dk\/blog\/wp-json\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"http:\/\/henrik.familiendamgaard.dk\/blog\/wp-json\/wp\/v2\/comments?post=393"}],"version-history":[{"count":4,"href":"http:\/\/henrik.familiendamgaard.dk\/blog\/wp-json\/wp\/v2\/posts\/393\/revisions"}],"predecessor-version":[{"id":396,"href":"http:\/\/henrik.familiendamgaard.dk\/blog\/wp-json\/wp\/v2\/posts\/393\/revisions\/396"}],"wp:attachment":[{"href":"http:\/\/henrik.familiendamgaard.dk\/blog\/wp-json\/wp\/v2\/media?parent=393"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/henrik.familiendamgaard.dk\/blog\/wp-json\/wp\/v2\/categories?post=393"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/henrik.familiendamgaard.dk\/blog\/wp-json\/wp\/v2\/tags?post=393"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}