Php 处理我的连接变量以供登录脚本使用
目前我正在使用Php 处理我的连接变量以供登录脚本使用,php,Php,目前我正在使用 $con=mysqli_connect("x","x","x","x"); 在登录脚本上处理我的数据库连接。然而,我正在尝试将其转换为OOP风格的方法,例如 $con = new dbclass(); $con->openDB();` 不过我在这方面运气不好 Warning: mysqli_real_escape_string() expects parameter 1 to be mysqli, object given in C:\xampp\htdocs\c\lo
$con=mysqli_connect("x","x","x","x");
在登录脚本上处理我的数据库连接。然而,我正在尝试将其转换为OOP风格的方法,例如
$con = new dbclass();
$con->openDB();`
不过我在这方面运气不好
Warning: mysqli_real_escape_string() expects parameter 1 to be mysqli, object given in C:\xampp\htdocs\c\login.php on line 103
Warning: mysqli_real_escape_string() expects parameter 1 to be mysqli, object given in C:\xampp\htdocs\c\login.php on line 104
Warning: mysqli_query() expects parameter 1 to be mysqli, object given in C:\xampp\htdocs\c\login.php on line 109
Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, null given in C:\xampp\htdocs\c\login.php on line 111
Acess denied, wrong username or password?
这就是我用来做这件事的方法
function openDB() {
$config = include("/assets/configs/db_config.php");
$conn = mysqli_connect($config["host"] , $config["username"], $config["password"],$config["dbname"]);
// 1. Create a database connection
if (!$conn)
{
$this->error_msg = "connection error could not connect to the database:! ";
return false;
}
$this->conn = $conn;
return true;
}
有人能提些建议吗。我是否需要在我的方法中的某个地方使用mysqli\u connect。或者更好,在我的db_配置文件中:
<?php
return array("host"=>"x", "dbname"=>"x", "username"=>"x", "password"=>"x");
mysqli_report(MYSQLI_REPORT_ERROR);
?>
include不会导致其中定义的变量成为数组-它们会自动导入到调用函数的作用域中。因为您没有在include文件中定义$config,所以永远不会访问数组
所以,$config=include'your_script.php'并不像您想象的那样
您应该如下更新函数,并将db_config.php中的数组定义更改为定义$host、$username、$password和$dbname
尽管如此,这并不是在OOP应用程序中处理DB连接的最佳方法。如果需要移动配置文件,则必须更新数据库类。最好将变量作为参数传递给openDB函数,然后从控制器中的某个配置脚本中检索它们
例如,以下是更好的做法:
function openDB($username, $password, $dbname, $host = 'localhost')
{
$conn = mysqli_connect($host, $username, $password, $dbname);
if(!$conn)
{
$this->error_msg = 'connection error could not connect to the database!';
return false;
}
$this->conn = $conn;
return true;
}
尝试只使用这个初始化,比如$db=newdb;无需打开数据库,它将已连接 编辑 下面是db_config.php的代码
$db_host = 'localhost';
$db_user = 'root';
$db_password = '';
$db_name = 'bs_admin';
class db
{
public $dbh;
public $error;
public $error_msg;
// Create a database connection for use by all functions in this class
function __construct()
{
require(dirname(dirname(__FILE__)) . '/config/db_config.php');
if($this->dbh = mysqli_connect($db_host, $db_user, $db_password, $db_name))
{
// Set every possible option to utf-8
mysqli_query($this->dbh, 'SET NAMES "utf8"');
mysqli_query($this->dbh, 'SET CHARACTER SET "utf8"');
mysqli_query($this->dbh, 'SET character_set_results = "utf8",' .
'character_set_client = "utf8", character_set_connection = "utf8",' .
'character_set_database = "utf8", character_set_server = "utf8"');
}
else
{
// Log an error if the connection fails
$this->error = true;
$this->error_msg = 'Unable to connect to DB';
}
// Add a row to any table
public function insert($table,$field_values)
{
$query = 'INSERT INTO ' . $table . ' SET ' . $field_values;
mysqli_query($this->dbh,$query);
}
}
看起来您刚刚复制了一个通用PHP数据库类。这并不能直接解决OP的问题,因为您没有提到或发布config/db_config.php的代码……我意识到现在已经有点晚了,但我无法在我的方法openDB$name、$pass、$dbname、$host password为空的方法中放置一个空密码对象,因此放置似乎是不可接受的。这有什么办法吗?感谢扩展openDB如下:openDB'root'、'myDB';应该足够了,不会引起任何问题。。。
$db_host = 'localhost';
$db_user = 'root';
$db_password = '';
$db_name = 'bs_admin';
class db
{
public $dbh;
public $error;
public $error_msg;
// Create a database connection for use by all functions in this class
function __construct()
{
require(dirname(dirname(__FILE__)) . '/config/db_config.php');
if($this->dbh = mysqli_connect($db_host, $db_user, $db_password, $db_name))
{
// Set every possible option to utf-8
mysqli_query($this->dbh, 'SET NAMES "utf8"');
mysqli_query($this->dbh, 'SET CHARACTER SET "utf8"');
mysqli_query($this->dbh, 'SET character_set_results = "utf8",' .
'character_set_client = "utf8", character_set_connection = "utf8",' .
'character_set_database = "utf8", character_set_server = "utf8"');
}
else
{
// Log an error if the connection fails
$this->error = true;
$this->error_msg = 'Unable to connect to DB';
}
// Add a row to any table
public function insert($table,$field_values)
{
$query = 'INSERT INTO ' . $table . ' SET ' . $field_values;
mysqli_query($this->dbh,$query);
}
}