Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#1 2011-06-08 18:40:46

HarryG
Member
Registered: 2011-01-12
Posts: 21

Using PHP / MySQL queries in pages and articles

I saw the article “http://textpattern.com/faq/34/how-do-i-use-php-code”.

I want to query a special database containing several linked tables, with over 1000 records of 30 columns in the main table. The results of the query will be displayed in tabular format on the output page. (It’s a product list for an e-commerce site.)

The FAQ says,

Please be aware that any PHP code you run from Textpattern, whether included directly in a page or in a separate file, will share database connections, function, variable and class names and so on with Textpattern. PHP doesn’t support separate namespaces, so it’s up to you to ensure there are no clashes.

Can someone explain what this means? I’ve read several on-line articles about databases and programming languages, and I am still confused by the term “namespace”.

Does it mean that if I want to use my own tables I need to import them into the main textpattern database? (There’s no problem in doing so, of course, but I thought this was a no-no.)

The quote above also seems to imply that I can’t invoke external .php files either—for the purpose of querying and displaying the data in my private database.

Can it be done if there is a separate, discrete path to the external files, such as “http://www.mysite.com/mydatabase/”?

Thanks,

-Harry G.

Last edited by HarryG (2011-06-08 18:45:23)

Offline

#2 2011-06-08 20:17:25

jakob
Admin
From: Germany
Registered: 2005-01-20
Posts: 5,001
Website GitHub

Re: Using PHP / MySQL queries in pages and articles

You should be able to do what you want with smd_query. For this you will need to have your tables in the same database as textpattern, the proviso being that you avoid conflicts with the existing textpattern tables or those of other plugins. There’s no need to establish separate database connections.


TXP Builders – finely-crafted code, design and txp

Offline

#3 2011-06-08 20:42:43

Bloke
Developer
From: Leeds, UK
Registered: 2006-01-29
Posts: 11,944
Website GitHub

Re: Using PHP / MySQL queries in pages and articles

HarryG wrote:

Can someone explain what this means?

As Jakob says, you can put the tables in the same database as Textpattern’s tables as long as the name of the tables differ (they more than likely will).

In terms of namespaces, currently TXP operates in the ‘global scope’: everything fights alongside PHP’s own functions and only one function of the same name can exist without error. A namespace is just a way to group related things — functions, classes, etc — together. Effectively, you are putting everything with the same namespace in a box and labelling that box with a unique name. Unlike in the global scope, you can have two identical function names as long as they are in two separate boxes. The only difference over non-namespaced access is that when you want something from either box you need to specify which one to work with first, e.g. txp\our_brilliant_function().

Unfortunately we don’t live in that world yet so you just need to make sure that your other app doesn’t have the same function names as TXP. If they do — PHP will certainly tell you — then you need to change either TXP or the other app. I had to do this when I was merging PunBB with TXP because they both used a function called message(). Luckily TXP’s had been deprecated so I could delete it.

In short you can — and probably should for ease of access with other tools like smd_query — put your tables in the same database as Textpattern. BTW, you can prefix TXP’s tables during setup to reduce the risk of clashing (that also allows you to have two TXP installs in one database) but it won’t help with the function names. The file paths make no difference, btw, it’s the contents of the files that counts.

Is that any clearer now or have I confounded you further?

Last edited by Bloke (2011-06-08 20:43:57)


The smd plugin menagerie — for when you need one more gribble of power from Textpattern. Bleeding-edge code available on GitHub.

Txp Builders – finely-crafted code, design and Txp

Offline

#4 2011-06-09 02:04:17

HarryG
Member
Registered: 2011-01-12
Posts: 21

Re: Using PHP / MySQL queries in pages and articles

Bloke wrote:

Is that any clearer now or have I confounded you further?

It’s clearer now. And my thanks to Bloke and Jakob for the info and the tip. I’m breathing easier. I thought I was going to have to write my own php-based quasi-CMS using server side includes…

Now, how do I go about setting this topic to “status: closed”?

-Harry G.

Offline

Board footer

Powered by FluxBB