 |
Smarty
The discussions here are for Smarty, a template engine for the PHP programming language. Dedicated server web hosting provided by Guru-host.eu. |
| View previous topic :: View next topic |
| Author |
Message |
Colino Smarty Rookie
Joined: 25 Sep 2009 Posts: 11
|
Posted: Mon Oct 26, 2009 1:32 pm Post subject: There is a plugin that connect smarty to MySql db? |
|
|
Is there a plugin that allows me to connect smarty to a mysql database, extract data from tables and populate them? _________________ ------------
http://intotheapp.blogspot.com
make everything with free software |
|
| Back to top |
|
elpmis Smarty Elite
Joined: 07 Jun 2007 Posts: 259
|
Posted: Mon Oct 26, 2009 2:14 pm Post subject: |
|
|
Smarty is "only" a template engine.
Connect and requests to databases should be done by php. Then the results can be assigned and displayed by Smarty. |
|
| Back to top |
|
ImNotAGeekReally Smarty Rookie
Joined: 27 Oct 2009 Posts: 5
|
Posted: Tue Oct 27, 2009 12:15 pm Post subject: Sort of |
|
|
If you need to access a MySQL db from a Smarty plugin, this is what I do.
PHP
=================
/* Do this in your config file */
define('SQL_HOST', 'localhost');
(do this for everything listed below)
SMARTY
=================
<?php
require_once(PRIVATE_DIR.'/lib/core/Database.class.php');
function smarty_function_funcName($params, &$smarty) {
$objDb = new Database(SQL_HOST, SQL_USER, SQL_PASSWORD, SQL_DB);
}
?>
You can then use $objDb as you would any normal database object. You have to you use define instead of $sql_host = 'localhost' as the defines are available globally.
Hope that helps
S |
|
| Back to top |
|
bimal Smarty Elite

Joined: 19 Apr 2007 Posts: 419 Location: Kathmandu, Nepal
|
Posted: Tue Oct 27, 2009 6:20 pm Post subject: Database resource may be available already |
|
|
Thats good - Some plugins may require Database as well.
Also here is an important thing to consider:
you might have already connected to the Database, earlier than PHP is executing the Smarty's plugins.
So, you should get rid of memorizing the host/username/password/database within the plugins. Do not connect again to the database to create a new resource.
If you are using simple mysql_connect(), ..., in your config files, you can write mysql_query() anywhere in the scripts later than the config. And If you are using some class files make sure that it does not call mysql_connect() again, when it is instantiated. Only one connection to the database is sufficient to render most of the pages.
Finally, Smarty Plugins are native PHP files, and can connect to the database as in normal way you write PHP scripts. But reduce the chances of mysql_connect() being called multiple times, with same host/username/password/database combinations within a page. _________________ Skype: pbimal
To hire instantly as a freelancer - https://www.odesk.com/o/profiles/users/_~~657b70cc7f2c616a/
Visit my website - http://bimal.org.np/ for more articles
800+ screenshots to learn about website mistakes: http://mistakes.sanjaal.com/ |
|
| Back to top |
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|