Categories > TinyButStrong general >

How do i call a database connection in subtemplate?

The forum is closed. Please use Stack Overflow for submitting new questions. Use tags: tinybutstrong , opentbs
By: handoko
Date: 2010-08-28
Time: 02:02

How do i call a database connection in subtemplate?

Dear skrol,

How do i call a database connection in sub templates using initial connection.

Currently i call the connection on every in sub template page not in initial stage, since the code cannot read the connection already set at the initial stage.
for example. in index.php i have already call this code;
include_once('include/ez_sql_core.php');
include_once('include/ez_sql_mysql.php');
include_once('include/constants.php');
$db = new ezSQL_mysql(DB_USER,DB_PASS,DB_NAME,DB_SERVER);

but when i call the sub template page, the connection is NULL. So for temporary, i put this code on every first code of the sub template page.

Do you have solution?

thanks

By: Skrol29
Date: 2010-08-28
Time: 23:36

Re: How do i call a database connection in subtemplate?

Hi Handoko,

This is a normal state of thinks, since the sub-template script is run in the context of a PHP function.

I can see two solutions:
1) Use a global variable for the $db connection, then you can retrieve it in the sub-template script.
or
2) Use a custom property of the TBS instance to refer to the $db connection, you can retrieve it in the sub-template script  because the current TBS instance is available in the sub-template script with the local variable named $TBS.
By: handoko
Date: 2010-08-29
Time: 00:16

Re: How do i call a database connection in subtemplate?

Thanks skrol,

For Solution one, i can not get the global $db connection in subtemplate script,

can you give an example of both solution/

Thanks
By: handoko
Date: 2010-08-29
Time: 00:28

Re: How do i call a database connection in subtemplate?

Dear Skrol,

Sorry, for solution one is okay, since i put global $db on subtemplate script i/o on main script.

i need example of solution two, maybe it is more complicated

Thanks
By: Skrol29
Date: 2010-08-29
Time: 00:31

Re: How do i call a database connection in subtemplate?

Did you know? MergeBlock can handle with MySQL ezSQL connections using property $db->dbh as the db source .
Example: $TBS->MergeBlock('b', $db->dbh, "SELECT * FROM table1");


Examples of solutions:

1)
in the main script:
--------------------
include_once('include/ez_sql_core.php');
include_once('include/ez_sql_mysql.php');
include_once('include/constants.php');
global $db;
$db = new ezSQL_mysql(DB_USER,DB_PASS,DB_NAME,DB_SERVER);

in the sub-template- script:
-------------------------------
global $db;
$db->get_row(...);

2)
in the main script:
--------------------
$TBS->db =& $db;

in the sub-template- script:
-------------------------------
$TBS->db->get_row(...);
By: handoko
Date: 2010-08-29
Time: 00:32

Re: How do i call a database connection in subtemplate?

Dear Skrol,

Sorry, for solution one is okay, since i put global $db on subtemplate script i/o on main script.

i need example of solution two, maybe it is more complicated

Thanks
By: TomH
Date: 2010-08-29
Time: 04:52

Re: How do i call a database connection in subtemplate?

Interesting tidbit you just drop in there Skrol29 about
$TBS->MergeBlock('b', $db->dbh, "SELECT * FROM table1");

Is there a similar TBS->dbh connector when using tbssql 3.0?

So that the $TBS->db->get_row(...); works for tbssql ???

By: Skrol29
Date: 2010-08-29
Time: 10:54

Re: How do i call a database connection in subtemplate?

Handoko:

2)
in the main script:
--------------------
include_once('include/ez_sql_core.php');
include_once('include/ez_sql_mysql.php');
include_once('include/constants.php');
$db = new ezSQL_mysql(DB_USER,DB_PASS,DB_NAME,DB_SERVER);

$TBS->db =& $db; // $TBS->db becomes a reference to $db

in the sub-template- script:
-------------------------------
// the local variable $TBS is available here, so you can also reach $TBS->db
$TBS->MergeBlock('b', $$TBS->db->dbh, "SELECT * FROM table1"); // exemple 2.a
...
$result = $TBS->db->get_row(...); // example 2.b
By: Skrol29
Date: 2010-08-29
Time: 11:05

Re: How do i call a database connection in subtemplate?

TomH:

Like the $db->dbh of ezSQL, TbsSQL has a $Db->id property which is the handle to the database connection for any type of database.
You can use it with MergeBlock only if TBS support the database in native, which is the case for MySQL.
However, TbsSQL has also a property $Db->TbsKey which makes MergeBlock to works whatever the type of database is.
$TBS->MergeBlock('b', $Db->TbsKey, "SELECT * FROM table1");