Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#1 2008-01-28 21:52:54

zeeforel
Member
From: Nieuwerkerk a/d IJssel, NL
Registered: 2004-10-16
Posts: 92
Website

Need some help calling second database in TxP template

Hi,

I am working on a small piece of PHP code that will allow me to include some information from a second MySQL database in my TxP based blog.

For that to happen I call from the TxP template:

<txp:php>

global $myspecialvar;
$myspecialvar = $GLOBALS[‘thisarticle’][‘category1’];
include(txpath.’/mylittleprogram.php’);

</txp:php>

Where mylittleprogram.php retrieves information from the second MySQL database and echo’s it into the output of the TxP database, that is, in the browser.

This code, by the way, is called between <txp:if_individual_article> ……….. </txp:if_individual_article> tags, to make sure I get the correct info in the $GLOBALS variable.

All seems to work pretty well but (in debugging mode) I get a lot of error messages, from which I read that TxP is trying to find a strange table in its TxP database like “table nameseconddatabase.textpattern does not exist” . So it appears that things are being mixed up and that is causing errors.

What can I do about this? I actually rather not interfere in the main TxP database.

Many thanks in advance.

UPDATE1 : on furter inspection I see that all txp tags called in the template after the php code inserted give errors. Does it have to do with cloding the second database in time?

UPDATE 2: tried this http://forum.textpattern.com/viewtopic.php?id=20265 but did not work (
should I have defined $txp_host, $txp_username, $txp_password etx. myself in mysql_pconnect($txp_host, $txp_username, $txp_password); mysql_select_db($txp_database); for this to work???

Last edited by zeeforel (2008-01-28 22:25:07)

Offline

#2 2008-01-29 06:20:16

Gocom
Developer Emeritus
From: Helsinki, Finland
Registered: 2006-07-14
Posts: 4,533
Website

Re: Need some help calling second database in TxP template

It’s way more easier if you do it as TXP’s admin side plugin – as then you have access to anything more easily. Also that kind of important stuff shouldn’t be included with txp:php to your pagetemplate. Use plugin and functions – more secure too.

Cheers!

Offline

#3 2008-01-29 07:15:30

zeeforel
Member
From: Nieuwerkerk a/d IJssel, NL
Registered: 2004-10-16
Posts: 92
Website

Re: Need some help calling second database in TxP template

Thanks, is that really the only way?

Offline

#4 2008-01-29 21:15:56

zeeforel
Member
From: Nieuwerkerk a/d IJssel, NL
Registered: 2004-10-16
Posts: 92
Website

Re: Need some help calling second database in TxP template

And if including the call to the second database in the plugin going to eleminate the errors I got?

I appreciate any input on this!

Thanks.

Offline

#5 2008-01-30 19:43:46

ruud
Developer Emeritus
From: a galaxy far far away
Registered: 2006-06-04
Posts: 5,068
Website

Re: Need some help calling second database in TxP template

No, rewriting this as a plugin doesn’t solve the problem.
I think your PHP script is re-using the MySQL link-identifier created by TXP instead of using it’s own unique link identifier.

Check your PHP script and the MySQL functions it uses. It should have something like:

$link = mysql_connect( ... );
mysql_select_db('database name', $link);
mysql_query('query', $link);

If those $link parts are missing, it re-uses the already existing MySQL link identifier that TXP created on startup and that explains why TXP itself suddenly tries to use a different database, because the mysql_select_db from your PHP script changed it.

When only one database is involved, using that $link parameter in MySQL functions is not needed, but when using two different databases, it has to be used to make sure the right connection is used to send the query.

Offline

#6 2008-01-31 06:18:08

zeeforel
Member
From: Nieuwerkerk a/d IJssel, NL
Registered: 2004-10-16
Posts: 92
Website

Re: Need some help calling second database in TxP template

Thanks Ruud,

That’s a very interesting part in this puzzle!

Cheers,
Marcel

Offline

#7 2008-02-04 22:57:28

zeeforel
Member
From: Nieuwerkerk a/d IJssel, NL
Registered: 2004-10-16
Posts: 92
Website

Re: Need some help calling second database in TxP template

Hi Ruud,

It only worked for me after using:

$link = mysql_pconnect( … );
mysql_select_db(‘database name’, $link);
mysql_query(‘query’, $link);

So mysql_pconnect( … ) and not mysql_connect( … );

Thanks again.

Offline

Board footer

Powered by FluxBB