从包含文件建立连接 [英] establishing connection from include file

查看:89
本文介绍了从包含文件建立连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

连接文件具有以下代码

<?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.

And i am including the above connection file in another page. I can't edit the config.php due to some reasons.

但是我如何与上述数组建立常规的mysqli连接?

But How can i do the regular mysqli connection with the above array ?

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

As in the above the i need the variable $mysqli as the to make the result. But my question is How can i get the values inside the array from config.php ?

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

Note : I can't make any changes or declare the $mysqli in the config.php

推荐答案

这应该对您有用.

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

这篇关于从包含文件建立连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆