Page 1 of 1

DB Connection in custom files

Posted: Fri Nov 24, 2017 10:27 pm
by mpol_ch

Hi,

I am using the following code in custom files to establish the database connection:

$conn = mysqli_connect("localhost","root","","rechnungsbox2018");

Include Common Files for the custom file are activated.
I have to change the username, password and database name manually when I change the database.

What should I use instead of this code to use the exsting database connection?

mpol_ch


Re: DB Connection in custom files

Posted: Fri Nov 24, 2017 10:37 pm
by mpol_ch

Hi,

the following code solved my problem:

$conn = new mysqli($EW_CONN["DB"]["host"], $EW_CONN["DB"]["user"], $EW_CONN["DB"]["pass"],$EW_CONN["DB"]["db"]);

mpol_ch


Re: DB Connection in custom files

Posted: Sat Nov 25, 2017 2:31 am
by kirondedshem

$conn = new mysqli($EW_CONN["DB"]["host"], $EW_CONN["DB"]["user"], $EW_CONN["DB"]["pass"],$EW_CONN["DB"]["db"]);

You should use DBhelper since it already does the database connection for you, check the home.php page in demo project or read about "Custom files" in help menu. The advantage is here you dont care what db you are connected to mysql,postgrsql,sql server etc, dbhelper always adjusts accordngly and they handle the database setup and connection for you.
BUt in summary in your custom file if you have common file included just do this

<?php
//make db connection depending on current config info
$db =& DbHelper(); // Create instance of the database helper class by DbHelper() (for main database) or DbHelper("<dbname>") (for linked databases) where <dbname> is database variable name
$sql = "select * from mytable";
//execute query
echo $db->ExecuteHtml($sql);
//other usages include
//$db->Execute($sql);
//$db->ExecuteScalar($sql);
//$db->ExecuteLoadRecordSet($sql);
//READ dbhlper.php file to find possible functions
?>

NOTE: If your custom file has common files included then even the default ew_execute and similar functions still work (if you do not have Linked Tables).