简体   繁体   English

从包含文件建立连接

[英]establishing connection from include file

The connection file has the following code 连接文件具有以下代码

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

    ),

);

And i am including the above connection file in another page. 我将上面的连接文件包含在另一个页面中。 I can't edit the config.php due to some reasons. 由于某些原因,我无法编辑config.php。

But How can i do the regular mysqli connection with the above array ? 但是我怎样才能与上面的数组进行常规的mysqli连接?

ie,

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

As in the above the i need the variable $mysqli as the to make the result. 如上所述,我需要变量$mysqli作为结果。 But my question is How can i get the values inside the array from config.php ? 但我的问题是如何从config.php中获取数组中的值?

Note : I can't make any changes or declare the $mysqli in the config.php 注意:我无法进行任何更改或在config.php中声明$mysqli

This should work for you. 这应该适合你。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM