Please enable JavaScript to view this site.

Navigation: » No topics above this level «

Using DAL in projects with multiple database connections

Scroll Prev Next More

 

Deprecated

Data Access Layer (DAL) is deprecated. While the existing code keeps working, we recommend switching to Database API.

 

In the Enterprise Edition of PHPRunner you can use multiple database connections in a single project. This article explains how you can access data from multiple databases in your events.

dal_mult_connections

Method 1: using DAL functions

There are three options to refer to a table:

 

1. Finds the first matching table by its name in all connections, starting with the primary connection.

 

global $dal;
$dal->Table("table")

 

2. Finds the tables by the table name and schema name.

 

global $dal;
$dal->Table("table","schema")

 

3. Finds the table by the table name, schema name and connection name. The schema name can be left empty, and the last parameter is the connection name as it appears on Datasource tables screen.

 

global $dal;
$dal->Table("table","schema","connection")

 

A complete code example:

 

global $dal;
$table = $dal->Table("cars", "", "cars at localhost");
$rs = $table->QueryAll();
while ($data = db_fetch_array($rs)) {
  echo $data["Make"].
  ", ".$data["Model"].
  "<br>";
}

Method 2: using free form SQL Queries

1. Here is how to update all cars where make is Audi and the YearOfMake equals 2002:

 

global $cman;
$cman->byName("cars at localhost")->exec("update carscars set yearofmake=2002 where make='Audi'");  

 

2. Example of a query returning data:

 

global $cman;
$connection = $cman->byName("cars at localhost");
$rs = $connection->query("select count(*) as c  from cars where make='Audi' ");
$data = $rs->fetchAssoc();
echo "Number of Audi cars listed: ".$data["c"];

See also:

QueryResult object: fetchAssoc()

Database API

Using DAL functions in projects with multiple database connections

Connecting to the database

Datasource tables screen

About Data Access Layer

 

Created with Help+Manual 7 and styled with Premium Pack Version 3 © by EC Software