{"id":465,"date":"2012-05-05T14:55:46","date_gmt":"2012-05-05T14:55:46","guid":{"rendered":"http:\/\/www.omniweb.com\/wordpress\/?p=465"},"modified":"2012-05-05T14:58:27","modified_gmt":"2012-05-05T14:58:27","slug":"connect-wordpress-to-a-second-database","status":"publish","type":"post","link":"https:\/\/www.omniweb.com\/wordpress\/?p=465","title":{"rendered":"Connect WordPress to a second database"},"content":{"rendered":"<p>While building a wordpress site and needed to work with a separate existing database.<br \/>\nAdded a function in wp-includes\/functions.php like this:<\/p>\n<p><code><br \/>\nfunction my_query($sql){<br \/>\n$db = @mysql_connect(\"my_DB_host\", \"DB_usr\", \"DB_pass\");<br \/>\nmysql_select_db(\"my_other_database\");<br \/>\n$result = mysql_query($sql) or die('Did not get required database result');<br \/>\nreturn $result;<br \/>\nmysql_close($db);<br \/>\n}<br \/>\n<\/code><\/p>\n<p>Then wherever I need to connect to this database I can use the function like this:<\/p>\n<p><code><br \/>\n$sql = sprintf(\"SELECT blah FROM table\");<br \/>\n$result = my_query($sql);<br \/>\n<\/code><\/p>\n","protected":false},"excerpt":{"rendered":"<p>While building a wordpress site and needed to work with a separate existing database. Added a function in wp-includes\/functions.php like this: function my_query($sql){ $db = @mysql_connect(&#8220;my_DB_host&#8221;, &#8220;DB_usr&#8221;, &#8220;DB_pass&#8221;); mysql_select_db(&#8220;my_other_database&#8221;); $result = mysql_query($sql) or die(&#8216;Did not get required database result&#8217;); return &hellip; <a href=\"https:\/\/www.omniweb.com\/wordpress\/?p=465\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[1],"tags":[],"_links":{"self":[{"href":"https:\/\/www.omniweb.com\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/465"}],"collection":[{"href":"https:\/\/www.omniweb.com\/wordpress\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.omniweb.com\/wordpress\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.omniweb.com\/wordpress\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.omniweb.com\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=465"}],"version-history":[{"count":4,"href":"https:\/\/www.omniweb.com\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/465\/revisions"}],"predecessor-version":[{"id":470,"href":"https:\/\/www.omniweb.com\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/465\/revisions\/470"}],"wp:attachment":[{"href":"https:\/\/www.omniweb.com\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=465"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.omniweb.com\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=465"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.omniweb.com\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=465"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}