KoolReport's Forum

Official Support Area, Q&As, Discussions, Suggestions and Bug reports.
Forum's Guidelines

Variable as a parameter in the connection string #1979

Closed Gabriela Matheus opened this topic on on Mar 18, 2021 - 3 comments

Gabriela Matheus commented on Mar 18, 2021

HI! In my company, customers have their own connections on SQL Server. So, I do a select and bring their connection data, but I'm having trouble passing these variables as parameters within the koolreport connectionstring.

This is my report.php:


<?php

include('Back_verifica_login.php');
include('Back_FuncaoConsulta.php');


$vardatabase=$_SESSION['BancoDeDados'];
$varUsername=$_SESSION['UID'];
$varPassword=$_SESSION['PWD']; 
$varservername=$_SESSION['ServerName'];

require_once "./vendor/autoload.php";

use \koolreport\KoolReport;
use \koolreport\processes\Filter;
use \koolreport\processes\TimeBucket;
use \koolreport\processes\Group;
use \koolreport\processes\Limit;

class Report extends KoolReport
{

protected function settings(){
        
    return array(
        "dataSources"=>array(
            "conexao"=>array(
                "connectionString"=>"sqlsrv:server=$varservername;Database=$vardatabase",
                "username"=>"$varUsername" ,
                "password"=>"$varPassword"

            )
        )
    );

}

Below is my setting. In report.php, I store the result of specific fields for my query within sessions (on another screen) and load them here by passing in variables, and inserting these variables in place of the connection fields.

I get the following error on my screen


Warning: Undefined variable $varservername in C:\xampp\htdocs\public\report.php on line 30

Warning: Undefined variable $vardatabase in C:\xampp\htdocs\public\report.php on line 30

Warning: Undefined variable $varUsername in C:\xampp\htdocs\public\report.php on line 31

Warning: Undefined variable $varPassword in C:\xampp\htdocs\public\report.php on line 32

Fatal error: Uncaught PDOException: SQLSTATE[42S02]: [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Nome de objeto 'vendas' inválido. in C:\xampp\htdocs\public\vendor\koolreport\core\src\datasources\PdoDataSource.php:428 Stack trace: #0 C:\xampp\htdocs\public\vendor\koolreport\core\src\datasources\PdoDataSource.php(428): PDOStatement->execute() #1 C:\xampp\htdocs\public\vendor\koolreport\core\src\KoolReport.php(315): koolreport\datasources\PdoDataSource->start() #2 C:\xampp\htdocs\public\indexReport.php(5): koolreport\KoolReport->run() #3 {main} thrown in C:\xampp\htdocs\public\vendor\koolreport\core\src\datasources\PdoDataSource.php on line 428

Remembering that I defined the variables up there. Am I putting things in the right order? Please help me, thank you.

KoolReport commented on Mar 18, 2021

You do:

protected function settings(){
    $vardatabase=$_SESSION['BancoDeDados'];
    $varUsername=$_SESSION['UID'];
    $varPassword=$_SESSION['PWD']; 
    $varservername=$_SESSION['ServerName'];
    return array(
        "dataSources"=>array(
            "conexao"=>array(
                "connectionString"=>"sqlsrv:server=$varservername;Database=$vardatabase",
                "username"=>"$varUsername" ,
                "password"=>"$varPassword"

            )
        )
    );

}
Gabriela Matheus commented on Mar 19, 2021

It work for me! Thanks!

KoolReport commented on Mar 19, 2021

That"s great

Build Your Excellent Data Report

Let KoolReport help you to make great reports. It's free & open-source released under MIT license.

Download KoolReport View demo
None yet

None