php 在php中包含数据库连接文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20787386/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Including database connection file in php
提问by Blank EDjok
I am creating a simple login feature in php for my website, now I tried everything before and it worked well, then I decided to reorganize my files by grouping all my functions together in one file, my database settings and connection in another file and my session configuration (for running on my localhost) in yet another file.
我正在为我的网站在 php 中创建一个简单的登录功能,现在我之前尝试了所有方法并且运行良好,然后我决定通过将我的所有功能组合在一个文件中来重新组织我的文件,我的数据库设置和连接在另一个文件和我的另一个文件中的会话配置(用于在我的本地主机上运行)。
The point for me doing this is simply to keep my code clean and organized and easy to understand for me in the future.
我这样做的目的只是为了让我的代码在未来保持干净、有条理和易于理解。
This is my login page:
这是我的登录页面:
<?php
include('session-config.php');
include('dbconnection.php');
include('functions.php');
include('password_hash_lib/password.php');
if (isset($_POST['data']))
{
$data = $_POST['data'];
$auth = json_decode($data);
$user_email = $auth->Email;
$user_pass = $auth->Password;
authenticate($user_email, $user_pass);
}
function authenticate($Email, $Password)
{
$HashedPassword = password_hash($Password, PASSWORD_DEFAULT);
$sql = "SELECT * FROM app_users WHERE user_email='$Email'";
$result = $db->query($sql);
$User = $result->fetch_object();
if ($User->user_email == '')
{
header("Location: login-page.html?msg=failed");
}
if (password_verify($Password, $User->user_password_hash))
{
$_SESSION["user_auth_details"] = $User->user_id . "+" . $User->user_email . '+' . $User->user_name . "+" . $User->user_display_image . "+" . $User->user_display_name;
header("Location:" . $_SESSION['page_url']);
}
else {
header("Location: login-page.html?msg=failed");
}
}
?>
And this is my database connection file:
这是我的数据库连接文件:
<?php
$db = new mysqli("xxxxxxxxxxxxxxxxxxxxxxx", "xxxxxxxxxxxxx", "xxxxxxxxxxxxx", "xxxxxxxxxxx");
if($db->connect_errno > 0){
die('Unable to connect to database [' . $db->connect_error . ']');
}
?>
As you can see I have included the dbconnection.php file in my login.php but whenever I try to call the authenticate()function, this error is returned:
如您所见,我在 login.php 中包含了 dbconnection.php 文件,但是每当我尝试调用该authenticate()函数时,都会返回此错误:
Notice: Undefined variable: db in C:\xampp\htdocs\xxxxxxxx\login.php on line 27
Fatal error: Call to a member function query() on a non-object in C:\xampp\htdocs\xxxxxxx\login.php on line 27
注意:未定义变量:db in C:\xampp\htdocs\xxxxxxxx\login.php 第 27 行
致命错误:在第 27 行的 C:\xampp\htdocs\xxxxxxx\login.php 中的非对象上调用成员函数 query()
Now I'm a bit confused about this, since I have $dbdefined in my dbconnection.php file and I have include that file in my login.php page, I expected that to work or am I wrong?
现在我对此有点困惑,因为我已经$db在我的 dbconnection.php 文件中定义了该文件并且我在我的 login.php 页面中包含了该文件,我希望它可以工作还是我错了?
采纳答案by Ahmad
You have to globalize the variable before using in a function.
Just add this line at the top of your function:
在函数中使用之前,您必须对变量进行全球化。
只需在函数顶部添加此行:
function authenticate($Email, $Password)
{
global $db;
$HashedPassword = password_hash($Password, PASSWORD_DEFAULT);
$sql = "SELECT * FROM app_users WHERE user_email='$Email'";
$result = $db->query($sql);
$User = $result->fetch_object();
...
回答by Nathan H
Add global $db;to the beginning of your authenticate function.
添加global $db;到您的身份验证功能的开头。
However, I strongly recommend you learn about PDO: http://wiki.hashphp.org/PDO_Tutorial_for_MySQL_Developers
但是,我强烈建议您了解 PDO:http: //wiki.hashphp.org/PDO_Tutorial_for_MySQL_Developers
Your code is currently unsafe
您的代码目前不安全
回答by Priya jain
check whether your database is connected or not. use connection code like this:
检查您的数据库是否已连接。使用这样的连接代码:
<?php
$link = mysql_connect('localhost', 'mysql_user', 'mysql_password');
if (!$link) {
die('Could not connect: ' . mysql_error());
}
echo 'Connected successfully';
mysql_close($link);
?>

