PHP函数访问数据库连接 [英] PHP Function Accessing Database Connection
本文介绍了PHP函数访问数据库连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何允许函数在不使用 GLOBAL 的情况下访问数据库连接?
How do I allow a function to access a database connection without using GLOBAL?
config.php
DEFINE ('DB_HOSTNAME', 'hostname');
DEFINE ('DB_DATABASE', 'database');
DEFINE ('DB_USERNAME', 'username');
DEFINE ('DB_PASSWORD', 'password');
$dbc = mysqli_connect(DB_HOSTNAME, DB_USERNAME, DB_PASSWORD, DB_DATABASE);
if(!$dbc) die("Unable to connect to MySQL: " . mysqli_error($dbc));
functions.php
functions.php
function something()
{
$info = mysqli_query($dbc, "SELECT info FROM text") or die("Error: ".mysqli_error($dbc));
}
以上给了我以下错误:mysqli_query() 期望参数 1 为 mysqli,
The above gives me the following error: mysqli_query() expects parameter 1 to be mysqli, null given in
推荐答案
使用函数参数
function something ($dbc) {
// your db code here
}
这篇关于PHP函数访问数据库连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文