database connection

Permalink
Concrete5 vers 8.3.1
Package Community Store vers 1.3.2

I have the below code placed in packages/community_store/single_pages/dashboard/store/products.php which works correctly

$connection = mysqli_connect($servername,$username,$password,$dbname) or die("Error connecting" . mysqli_error($connection));
    $sql = "select pID,pName,pDesc,pPrice from CommunityStoreProducts";
    $result = mysqli_query($connection, $sql) or die("Error in Selecting " . mysqli_error($connection));
    $emparray = array();
    while($row = mysqli_fetch_assoc($result))
    {
        $emparray[] = $row;
    }
    $fp = fopen('application/themes/theme/json.json', 'w');
    fwrite($fp, json_encode($emparray,JSON_NUMERIC_CHECK));
    fclose($fp);
    mysqli_close($connection);


However for concrete5 it is not recommended to use mysqli_connect and instead use below but I am unable to get this to work , please can anyone advise or have an example I could use.

$app = \Concrete\Core\Support\Facade\Facade::getFacadeApplication();
$db = $app->make(Connection::class)->connection();
or 
 $db = \Database::connection();

 
MrKDilkington replied on at Permalink Reply
MrKDilkington
Hi silko,

Have you reviewed the database access documentation?
https://documentation.concrete5.org/developers/database-management/a...
ramonleenders replied on at Permalink Reply
ramonleenders
In /application/config/database.php, you can make multiple connections in the "connections" array. Like so:
<?php
return [
   'default-connection' => 'concrete',
   'connections'        => [
      'concrete' => [
         'driver'   => 'c5_pdo_mysql',
         'server'   => 'localhost',
         'database' => 'my_database',
         'username' => 'root',
         'password' => '',
         'charset'  => 'utf8',
      ],
      'another_connection' => [
         'driver'   => 'c5_pdo_mysql',
         'server'   => 'localhost',


And if I'm not mistaken, you can do Database::connection("another_connection"), to connect with the other credentials (instead of "default-connection" that falls back to "concrete").