DB Connection in custom files

This public forum is for user-to-user discussions of PHPMaker. Note that this is not support forum.
Post Reply
mpol_ch
User
Posts: 877
Location: Switzerland

DB Connection in custom files

Post 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


mpol_ch
User
Posts: 877
Location: Switzerland

Post 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


kirondedshem
User
Posts: 642

Post 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).


Post Reply