处理我的连接变量以供登录脚本使用


Handling my connection variable for login script use

当前我正在使用

$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_connect()吗。或者更好的是,在我的db_config文件中:

<?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)。

function openDB() 
{
     include("/assets/configs/db_config.php");
     $conn = mysqli_connect($host, $username, $password, $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;
}

话虽如此,这并不是在OOP应用程序中处理DB连接的最佳方式。如果需要移动配置文件,则必须更新Database类。最好将变量作为参数传递给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=new db()一样只使用这个initiate;无需打开数据库,它将已经连接

编辑
这是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);       
}
}