1

Currently I am using

$con=mysqli_connect("x","x","x","x");

to handle my database connection on the login script. However, I'm trying to transition this to an OOP style approach such as

$con = new dbclass();
$con->openDB();`

I'm not having any luck with this though.

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?

This is the method I'm using to do this.

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;
}

Can anyone make any suggestions. Do I need to use mysqli_connect() somewhere within my method. Or even better, within my db_config file :

<?php
     return array("host"=>"x", "dbname"=>"x", "username"=>"x", "password"=>"x");


mysqli_report(MYSQLI_REPORT_ERROR);


?>
4

2 回答 2

0

尝试使用这个只启动像 $db = new db(); 无需打开数据库它已经连接

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

}
于 2013-07-30T11:04:54.293 回答
0

include()不会导致其中定义的变量成为数组 - 它们会自动导入到您调用函数的范围内。由于您没有$config在包含文件中定义,因此您永远不会访问该数组。

所以,$config = include('your_script.php')不要做你可能认为它会做的事情。

您应该按如下方式更新函数(并将内部的数组定义更改为db_config.phpdefine $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 应用程序中处理数据库连接的最佳方式。如果需要移动配置文件,则必须更新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;
}
于 2013-07-30T11:03:18.303 回答