建立连接从包括文件(establishing connection from include fil

2019-10-21 12:39发布

连接文件具有下面的代码

<?php
return array(
    'connections' => array(
        'mysql' => array(
            'driver'    => 'mysql',
            'host'      => 'localhost',
            'database'  => 'dbname',
            'username'  => 'root',
            'password'  => 'dbpass',
            'charset'   => 'utf8',
            'collation' => 'utf8_unicode_ci',
            'prefix'    => '',
        ),

    ),

);

而我,包括其他页面上的连接文件。 我不能编辑config.php文件由于某些原因。

但我怎么能做到上述阵列正规的mysqli连接?

<?php
include('../config/config.php');
//$mysqli = new mysqli("localhost", "root", "", "wolly");  // need to do the connection
$query = "SELECT * FROM action";
$result = $mysqli->query($query)

正如上面我所需要的变量$mysqli作为,使结果。 但我的问题是,我怎样才能从config.php文件中的数组中值是多少?

注:我不能做任何修改或声明$mysqli的config.php文件

Answer 1:

这应该为你工作。

$array = include('../config/config.php');

$mysql_conn = $array['connections']['mysql'];

$mysqli = new mysqli($mysql_conn['host'], $mysql_conn['username'],
                     $mysql_conn['password'], $mysql_conn['database']);

$query = 'SELECT * FROM action';
$result = $mysqli->query($query);


文章来源: establishing connection from include file