Textpattern CMS support forum
You are not logged in. Register | Login | Help
- Topics: Active | Unanswered
Pages: 1
Connect to a non-TXP db
I’m working on a project that is using two seperate databases: one for Textpattern and another used to manage some other content. I’m trying to write a plugin that pulls information from the non-TXP db and returns it in a Textpattern page but when I open the connection to the other db and select a table I take over Textpattern’s connection.
So, what is the best way to connect to the second db? Or, should I add the tables from the second db to the Textpattern db and just use the existing connection? (I’d prefer to keep them separate if possible.)
Manfre seemed to have put together a mod that did something like I’m after but it’s two years old and the link’s dead.
Here is the code I’m using:
// Set up db connection
$conn = mysql_connect($server, $username ,$password);
// Check the connection.
if(!$conn){
return "<p>Our apologies! We couldn't connect to the database.</p>";
}
// Select database
if(!mysql_select_db($dbase,$conn)){
return "<p>Our apologies! We couldn't select the specified database.</p>";
}
Offline
Re: Connect to a non-TXP db
Bump. Any advice?
Offline
Re: Connect to a non-TXP db
You can explicitly close your connection, once you have the data fetched. That way textpattern will go back to using the only remaing open connection.
It’s (generally) not a good idea to open multiple database connections on every pageview, as you can easily run into connection limits etc. If you do not want to merge all tables into a single db, you could look into fetching and storing in certain timeintervalls (caching if you will). You could try wrapping it into zem_cache for a very simple solution.
Offline
Re: Connect to a non-TXP db
I went ahead and moved my tables into my Textpattern database. I guess I’m still a bit of a beginner when it comes MySQL so I was unsure if this presented any hazard to the TXP db. Is there any compelling reason why I shouldn’t combine them or is the potential for problems so minor that it’s not really an issue?
Last edited by jdykast (2006-10-28 00:44:09)
Offline
Re: Connect to a non-TXP db
I would also like to connect to a different database powering another Textpattern site on the same server, but keep getting errors.
Would anyone be willing to give a fairly straight-forward explanation of what is required for caching?
Here’s my situation:
I have two sites, both using Textpattern.
I post images with Textpattern-generated thumbnails in site A.
I would like to display the thumbnail of the latest image posted to site A on the homepage of site B.
I try connecting to the database of site A from site B with the following code:
<txp:php>
$link = mysql_connect('localhost', 'username', 'password');
if (!$link) {
die('Could not connect: ' . mysql_error());
}
$db_selected = mysql_select_db('siteA-database', $link);
if (!$db_selected) {
die ('Can\'t use siteA-database : ' . mysql_error());
}
$query="SELECT * FROM txp_image ORDER BY id DESC limit 1";
$result=mysql_query($query);
$row = mysql_fetch_array($result);
mysql_close($link);
</txp:php>
<div id="thumb">
<a href="http://siteA.com"><img src="http://siteA.com/images/<txp:php> echo $row["id"]; </txp:php>t<txp:php> echo $row["ext"]; </txp:php>" alt="<txp:php> echo $row["alt"]; </txp:php>" /></a>
</div>
The error I get is the page fails to load completely with no thumbnail showing.
I assume site B's Textpattern database call is interfering with my database call to site A.
Any tips much appreciated.
Cheers!
Offline
Pages: 1