从包含文件建立连接
establishing connection from include file
连接文件有以下代码
<?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
这应该适合你。
$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);
连接文件有以下代码
<?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
这应该适合你。
$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);