{"id":2607,"date":"2012-01-30T01:12:45","date_gmt":"2012-01-30T07:12:45","guid":{"rendered":"http:\/\/bucktownbell.com\/?p=2607"},"modified":"2012-10-14T23:54:29","modified_gmt":"2012-10-15T04:54:29","slug":"connecting-to-an-external-database-from-within-a-wordpress-post-tyssen-design","status":"publish","type":"post","link":"http:\/\/bucktownbell.com\/?p=2607","title":{"rendered":"Connecting to an external database from within a WordPress post"},"content":{"rendered":"<p><a href=\"http:\/\/www.tyssendesign.com.au\/articles\/cms\/connecting-to-external-database-from-within-wordpress\/\">Connecting to an external database from within a WordPress post \u2014 Tyssen Design<\/a>.<\/p>\n<p>Hi Phil,<\/p>\n<p>At the top of the post, I included: <code><br \/>\n&lt;?php<br \/>\ninclude('\/path\/to\/database\/connection.php');<br \/>\n? <\/code><\/p>\n<p>and that file looked like:<code><br \/>\n&lt;?php<br \/>\nfunction runSQL($rsql) {<br \/>\n$rootpasswd='yourPasswordHere';<br \/>\n$user='yourUserHere';<br \/>\n$db='yourDBhere';<br \/>\n$dbcnx = @mysql_connect('localhost',$user,$rootpasswd,true);<br \/>\nif (!$dbcnx) {<br \/>\necho '&lt;p&gt;Unable to connect to the database server at this time.&lt;\/p&gt;';<br \/>\nexit();<br \/>\n}<br \/>\nmysql_select_db($db, $dbcnx);<br \/>\n$result = mysql_query($rsql) or die ('test');<br \/>\nreturn $result;<br \/>\nmysql_close($connect);<br \/>\n}<br \/>\n}<br \/>\n<\/code><\/p>\n<p>Then to loop through the results:<code><br \/>\n$sql = \"Your SQL statement goes here\";<br \/>\n$result = runSQL($sql);<br \/>\nwhile ($row = mysql_fetch_array($result)) {<br \/>\nDo stuff;<br \/>\n}<\/code><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Connecting to an external database from within a WordPress post \u2014 Tyssen Design. Hi Phil, At the top of the post, I included: &lt;?php include(&#8216;\/path\/to\/database\/connection.php&#8217;); ? and that file looked like: &lt;?php function runSQL($rsql) { $rootpasswd=&#8217;yourPasswordHere&#8217;; $user=&#8217;yourUserHere&#8217;; $db=&#8217;yourDBhere&#8217;; $dbcnx = &hellip; <a href=\"http:\/\/bucktownbell.com\/?p=2607\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[54],"tags":[156,55,131],"class_list":["post-2607","post","type-post","status-publish","format-standard","hentry","category-programming","tag-databases","tag-php","tag-wordpress"],"_links":{"self":[{"href":"http:\/\/bucktownbell.com\/index.php?rest_route=\/wp\/v2\/posts\/2607","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/bucktownbell.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/bucktownbell.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/bucktownbell.com\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/bucktownbell.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2607"}],"version-history":[{"count":4,"href":"http:\/\/bucktownbell.com\/index.php?rest_route=\/wp\/v2\/posts\/2607\/revisions"}],"predecessor-version":[{"id":7383,"href":"http:\/\/bucktownbell.com\/index.php?rest_route=\/wp\/v2\/posts\/2607\/revisions\/7383"}],"wp:attachment":[{"href":"http:\/\/bucktownbell.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2607"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/bucktownbell.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2607"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/bucktownbell.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2607"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}