Connecting to an external database from within a WordPress post — Tyssen Design.
Hi Phil,
At the top of the post, I included:
<?php
include('/path/to/database/connection.php');
?
and that file looked like:
<?php
function runSQL($rsql) {
$rootpasswd='yourPasswordHere';
$user='yourUserHere';
$db='yourDBhere';
$dbcnx = @mysql_connect('localhost',$user,$rootpasswd,true);
if (!$dbcnx) {
echo '<p>Unable to connect to the database server at this time.</p>';
exit();
}
mysql_select_db($db, $dbcnx);
$result = mysql_query($rsql) or die ('test');
return $result;
mysql_close($connect);
}
}
Then to loop through the results:
$sql = "Your SQL statement goes here";
$result = runSQL($sql);
while ($row = mysql_fetch_array($result)) {
Do stuff;
}