Categories > TinyButStrong general >

Example of class for MySQL DB

The forum is closed. Please use Stack Overflow for submitting new questions. Use tags: tinybutstrong , opentbs
By: teknojunki
Date: 2005-11-27
Time: 04:16

Example of class for MySQL DB

I was wondering if you could show me your example of using a class for a MySQL db connection. I have an existing application that uses OOP and my class is not working properly with $TBS->ObjectRef. This is probably do to my function layout.

Thanks in advance
By: Skrol29
Date: 2005-11-28
Time: 01:48

Re: Example of class for MySQL DB

Hello,

I can give an example but this should be useless because MySQL is supported in native with TBS.
By: teknojunki
Date: 2005-11-28
Time: 02:12

Re: Example of class for MySQL DB

I think I missing something then. If TBS has the built in support, then where do I configure the host, username, password, and dbname?

These are defined in my dbConnect class and according to your instructions I thought I could call that class/method to connect/fetch. Please clarify this for me.

Thank you for your help.
By: Anonymous
Date: 2005-11-29
Time: 04:03

Re: Example of class for MySQL DB

To clarify a bit more;

Here is my class file for the dbConnect:
class dbConnect extends sysComp {

    public $theQuery;
    public $link;

    //*** Function: dbConnect, Purpose: Connect to the database ***
    function dbConnect(){
        // Load settings from parent class sysComp
        $settings = sysComp::getSettings();
        // Get the main settings from the array we just loaded
        $host = $settings['dbhost'];
        $db = $settings['dbname'];
        $user = $settings['dbusername'];
        $pass = $settings['dbpassword'];
        // Connect to the database
        $this->link = mysql_connect($host, $user, $pass);
        mysql_select_db($db);
        register_shutdown_function(array(&$this, 'close'));
    }

    //*** Function: query, Purpose: Execute a database query ***
    function query($query) {
        $this->theQuery = $query;
        return mysql_query($query, $this->link);
    }

    //*** Function: fetchArray, Purpose: Get array of query results ***
    function fetchArray($result) {
        return mysql_fetch_array($result);
    }
   
    //*** Function: getRows, Purpose: Get number of rows from query results ***
    function getRows($result) {
        return mysql_num_rows($result);
    }

    //*** Function: close, Purpose: Close the DB connection ***
    function close() {
        mysql_close($this->link);
    }
   
}

?>

When I try to set up a $TBS->MergeBlock('blkname', $connect, 'SELECT * FROM tbl1');

The connection is not setting up toexecute the SQL.

I can execute several statements and retrieve data just fine for anything I need, but I guess I am not understanding your instructions as to how to perform the mergeblock.
By: Skrol29
Date: 2005-11-30
Time: 16:08

Re: Example of class for MySQL DB

Hello,

> If TBS has the built in support, then where do I configure
> the host, username, password, and dbname?

TBS doesn't performe any connection. Not for MySQL and neither for any other data source. You have to open your connexion, then TBS can use it.

Regarding to your code, you can do the following:
$connect = new dbConnect;
$connect->dbConnect();
...
$TBS->MergeBlock('blkname', $connect->link, 'SELECT * FROM tbl1');

or
$connect = new dbConnect;
$connect->dbConnect();
...
$TBS->MergeBlock('blkname', 'mysql', 'SELECT * FROM tbl1');
This second issue is possible because Php enables you to use MySql functions without precising the connection Id.
By: Johnny
Date: 2006-06-15
Time: 12:11

Re: Example of class for MySQL DB

I use PEAR with TinyButStrong
it's quite simple as follows:
<?php
require_once('DB.php'); //PEAR::DB class
include_once('include/tbs_class.php'); //TinyButStrong Template Engine

$TBS = new clsTinyButStrong ;
$TBS->LoadTemplate('template.html','BIG5') ;

$title='TinyButStrong with PEAR DEMO';

//query data records--------------------->
$dsn='mysql://root@localhost/cyberfair';
$db =& DB::connect($dsn);
if(PEAR::isError($db)){
    die($db->getMessage());
}
$query="select * from infocertificate where TeamID like 'C0611%'";
$TBS->MergeBlock('blk1','mysql',$query);
//<---------------------------------------
   

$TBS->Show();
?>
you just connect to a database, and whatever connection variable you use, TBS will just know it, you don't have to care about it much !!!