Using SQL Data Sources
Our sample projects and report templates can help you learn the basics of working with our products.This example demonstrates how to handle SQL queries and parameters in a Stimulsoft Viewer for PHP. First, include the Stimulsoft libraries:
Create a Viewer object and configure JavaScript options such as relative paths and additional HTML elements:
Define viewer events before processing. In this example, onBeginProcessData is used to customize the database connection, SQL queries, and query parameters:
Process requests on the server side using process():
Create a report object and load a report (or dashboard) template using loadFile(). The report will be loaded into a JavaScript object on the client side:
Finally, display the viewer using printHtml():
<?php
require_once '../vendor/autoload.php';
use Stimulsoft\Events\StiDataEventArgs;
use Stimulsoft\Report\StiReport;
use Stimulsoft\Viewer\StiViewer;
?>
Create a Viewer object and configure JavaScript options such as relative paths and additional HTML elements:
$viewer = new StiViewer();
$viewer->javascript->relativePath = '../';
$viewer->javascript->appendHead('<link rel="shortcut icon" href="/../favicon.ico" type="image/x-icon">');
Define viewer events before processing. In this example, onBeginProcessData is used to customize the database connection, SQL queries, and query parameters:
$viewer->onBeginProcessData = function (StiDataEventArgs $args) {
if ($args->connection == 'MySQL')
$args->connectionString = 'Server=localhost; Database=northwind; UserId=root; Pwd=;';
if ($args->dataSource == 'MyDataSource')
$args->queryString = 'SELECT * FROM MyTable';
if ($args->dataSource == 'MyDataSourceWithParams') {
$args->parameters['Parameter1']->value = 'TableName';
$args->parameters['Parameter2']->value = 10;
$args->parameters['Parameter3']->value = '2019-01-20';
}
};
Process requests on the server side using process():
$viewer->process();
Create a report object and load a report (or dashboard) template using loadFile(). The report will be loaded into a JavaScript object on the client side:
$report = new StiReport();
$report->loadFile('../reports/ManufacturingSQL.mrt');
$viewer->report = $report;
Finally, display the viewer using printHtml():
$viewer->printHtml();