For the article I wrote recently on web design galleries, I compiled a table of the galleries in a MySQL database. To be able to display the table and the associated charts and other statistics that went with it, I needed to be able to connect to this external database from within Wordpress.
A post on the Wordpress support forum suggested creating a separate template and adding an include which contained the database connection string to that template. That’s what I did for the page to display the table as the design for the page called for a single column rather than two like the rest of the site, but you can only assign different templates to pages, not posts, within Wordpress; posts in Wordpress are all displayed by single.php. That wasn’t an issue though because I just placed a conditional at the top of the single template that said if(is_single(XXX)) include('database_connection.php');.
That worked fine for the post, but what I didn’t realise at first was that it had broken the site’s feed because the database connection include wasn’t available to the files that create the feed which can be found in /wp-includes/ – feed-atom.php, feed-rdf.php, feed-rss.php and feed-rss2.php. So when the feed file was parsing the post and came to a call to a PHP function contained in an include file that it didn’t have access to, it threw an error.
The solution to this was simple too though: just take the include calls out of the template and include them in the actual post themselves (before everything else) as everything included in the post gets passed to the feed-creating files. I was already using the Exec-PHP plugin to enable the use of PHP within my posts, so it just meant linking to the includes from a different part of the page; a part of the page to which the feed files had access.







Okay sooo how did you do this? Any examples I could find to help with something that I am currently working on?
- Phil
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;
}
Totally awesome!
I was looking for something like this. Want to turn it into a function to call my database options on a custom theme I released.
Now to figure on how to implement
hi,
I still get the error
Warning: mysql_error(): 10 is not a valid MySQL-Link resource
which is triggered differently.
I just wonder how to debug such an error since it points to a sub sub function.
I was able to solve my problem modifying the function on my database manipulation to include the mysql_select_db function and commenting the mysql_close function safely.
You really want to use one of the Wordpress path constants in order to include your custom php file.
http://codex.wordpress.org/Determining_Plugin_and_Content_Directories
That’s assuming the database connection file resides within your Wordpress directory structure, which might not necessarily be the case.
every thing is perfectly OK .but i don’t understand, in which editor do i modify this?
please give me the procedure if i am implementing dB for a login form for both admin remote server as well as localhost.
Hi Ram, I’m sorry but I don’t understand the question.
You could easily implement this in your functions.php file and create a short code for use in your post. Then you wouldn’t need phpexec.
Sounds like a good idea and also a good idea for someone to write a blog post about.