Textpattern CMS support forum
You are not logged in. Register | Login | Help
- Topics: Active | Unanswered
#1 2010-11-28 05:01:16
- kevinpotts
- Member
- From: Ghost Coast
- Registered: 2004-12-07
- Posts: 370
Semi-Advanced Q: Can I embed PHP referencing another MySQL DB?
Right now in a TXP site I have a set of complicated forms that are writing to a database. I initially set these up to be writing to tables inside the same DB where the TXP records reside, and then it occurred to me that maybe putting web form data with application data in the same DB might now be a good idea. (Maybe it is; I dunno.)
Basically, I have an article with this code in it:
<txp:php> include 'forms/form_trial-membership.php'; </txp:php>
When the form accesses and writes info to tables inside the singular DB, all is right with the world. But when I try to reference another DB from the PHP script, basically jumping outside the entire TXP world for a moment before diving back in, all hell breaks loose and I get things like this:
Tag error: <txp:article_custom section='<txp:section />' form="subnav_single_level1" sort="custom_8 asc" limit="999" /> -> Textpattern Warning: Table 'client_dev_hrf_forms.textpattern' doesn't exist
(“client_dev_hrf_forms” being the name of my new rogue DB.)
I tried dropping in mysql_close() and other functions to my script, but nothin’. Looking for help from some experts. Maybe I am stuck with one DB.
Kevin
(graphicpush)
Offline
Re: Semi-Advanced Q: Can I embed PHP referencing another MySQL DB?
Unless you have a really compelling reason, I would go ahead and use mem_simple_form
and smd_query
to write, edit and delete records in a custom table within textpattern’s own database. It is so simple.
Offline
Re: Semi-Advanced Q: Can I embed PHP referencing another MySQL DB?
kevinpotts
I’d echo what aslsw66 says about keeping stuff in a different table in the same database. It is easier. But if you absolutely must do it in a separate DB you’ll have to go through some PHP drudgery of opening a connection to the database (cf. mysql_connect) and related functions. e.g.
$ext_db = mysql_connect ('db_name', 'mysql_user', 'mysql_password');
$result = mysql_query('SELECT whatever FROM wherever', $ext_db);
...
That should work but I’ve never tried it from inside TXP.
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 2010-11-29 01:30:49
- kevinpotts
- Member
- From: Ghost Coast
- Registered: 2004-12-07
- Posts: 370
Re: Semi-Advanced Q: Can I embed PHP referencing another MySQL DB?
I’ve done all the opening and closing of separate DBs … if I run the PHP script alone, outside Textpattern, it all works just fine and dandy.
Here is the root of my question: is it bad practice to mix customer/user-submitted data with application data? I am not an app builder, so I am not sure if that is good or bad practice. Looking for real-world advice.
Kevin
(graphicpush)
Offline
Re: Semi-Advanced Q: Can I embed PHP referencing another MySQL DB?
Well, every time a user posts an article or a comment, that’s user-submitted data that gets added to the app’s database. So, not quite sure what distinction you’re trying to draw here.
Offline
Re: Semi-Advanced Q: Can I embed PHP referencing another MySQL DB?
A couple of things in favour of using a single database include;
- easier maintenance if you can use a plugin (eg. coding is easier to read and modify),
- easier database maintenance (eg. you only need to back up one database).
But I’m not sure if there are any performance issues associated with database size. Luckily, my stuff is all small beer.
Offline
#7 2010-11-29 04:25:40
- kevinpotts
- Member
- From: Ghost Coast
- Registered: 2004-12-07
- Posts: 370
Re: Semi-Advanced Q: Can I embed PHP referencing another MySQL DB?
OK, let’s say I have the live environment and a dev environment. Typically, I port the entire DB back and forth for major updates for clients. But if I do this, I will be potentially losing customer data saved in other tables. it’s not a big deal to handle this on a more manual basis.
My point: I am not an application developer. Just looking for best practice.
Kevin
(graphicpush)
Offline
Re: Semi-Advanced Q: Can I embed PHP referencing another MySQL DB?
It’s a simple matter to import only the Txp tables and leave other tables untouched.
Offline