从包含文件建立连接

时间:2015-01-06 00:33:06

标签: php mysqli include

连接文件具有以下代码

<?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中获取数组中的值?

注意:我无法进行任何更改或在config.php中声明$mysqli

1 个答案:

答案 0 :(得分:3)

这应该适合你。

$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);