Skip to content
This repository was archived by the owner on Mar 29, 2023. It is now read-only.

Connecting to a database

Johan Strydom edited this page Aug 13, 2021 · 3 revisions

Tsama uses PDO for its databse connection. Read more about PDO here.

Before you can connect to your database you need to configure the /conf/db.conf.php file.

<?php 
//Your db configuration for PDO
$_DB_CONFIG['Driver'] = 'mysql';
$_DB_CONFIG['Host'] = 'localhost';
$_DB_CONFIG['Username'] = 'Username';
$_DB_CONFIG['Password'] = 'Password';
$_DB_CONFIG['Name'] = 'Database_Name';
$_DB_CONFIG['PORT'] = '';
$_DB_CONFIG['Version'] = '0.1';
?>

$_DB_CONFIG['Driver'] = 'mysql';

This is the database driver you are using. most of the time it is mysql, even if your using mariadb.

$_DB_CONFIG['Host'] = 'localhost';

Specify the host of your database.

$_DB_CONFIG['Username'] = 'Username';

Specify the username for the connection to the db

$_DB_CONFIG['Password'] = 'Password';

Specify the password for the user

$_DB_CONFIG['Name'] = 'Database_Name';

Specify the database name

$_DB_CONFIG['PORT'] = '3333';

Optionally specify a port. If set Tsama will use this port to connect else it will not specify a port and PDO will use the default port.

$_DB_CONFIG['Version'] = '0.1';

Specify the db version

Connecting to the Database

Tsama have a Database class called TsamaDatabase which will use the /conf/db.conf.php file to connect to the database.

In order to connect, simply do the following:

$db = new TsamaDatabase();
if(!$db->Connect()){
  echo '<div class="alert alert-danger" role="alert">Error encountered. Please try again later.</div>';
  return;
}

NEXT » Query a database table «

Clone this wiki locally