无法使用 php 连接到 mysql

Unable to connect to mysql using php

尝试通过 PHP 连接时显示

警告:mysqli_connect(): (HY000/1044): 拒绝用户 ''@'localhost' 访问 C:\wamp\www\booked\lib\Database\MySQL\MySqlConnection 中的数据库 'bookedscheduler' .php 第 52 行

警告:mysqli_select_db() 期望参数 1 为 mysqli,第 53 行 C:\wamp\www\booked\lib\Database\MySQL\MySqlConnection.php 中给出的布尔值

警告:mysqli_set_charset() 期望参数 1 为 mysqli,第 54 行 C:\wamp\www\booked\lib\Database\MySQL\MySqlConnection.php 中给出的布尔值

无法修改 header 信息 - headers 已经发送(输出开始于 C:\wamp\www\booked\lib\Database\MySQL\MySqlConnection.php:53)在 C:\wamp\www\booked\Pages\Page.php 第 138 行

我的config.php 数据库连接

$conf['settings']['database']['type'] = 'mysql';

$conf['settings']['database']['user'] = 'booked_user'; 

$conf['settings']['database']['password'] = '';

$conf['settings']['database']['hostspec'] = '127.0.0.1'; 

$conf['settings']['database']['name'] = 'bookedscheduler';

Mysqlconnection.php 文件

class MySqlConnection implements IDbConnection
{
private $_dbUser = '';
private $_dbPassword = '';
private $_hostSpec = '';
private $_dbName = '';

private $_db = null;
private $_connected = false;

/**
 * @param string $dbUser
 * @param string $dbPassword
 * @param string $hostSpec
 * @param string $dbName
 */
public function __construct($dbUser, $dbPassword, $hostSpec, $dbName)
{
    $this->_dbUser = $dbUser;
    $this->_dbPassword = $dbPassword;
    $this->_hostSpec = $hostSpec;
    $this->_dbName = $dbName;
}

public function Connect()
{
    if ($this->_connected && !is_null($this->_db))
    {
        return;
    }

    $this->_db = mysqli_connect($this->_hostSpec, $this->_dbUser, $this->_dbPassword,$this->_dbName);
    $selected = mysqli_select_db($this->_db, $this->_dbName);
    mysqli_set_charset($this->_db, 'utf8');

    if (!$this->_db || !$selected)
    {
        throw new Exception("Error connecting to database\nError: " . mysql_error());
        Log::Error("Error connecting to database\n%s",  mysql_error());
    }

    $this->_connected = true;
}

public function Disconnect()
{
    mysqli_close($this->_db);
    $this->_db = null;
    $this->_connected = false;
}

public function Query(ISqlCommand $sqlCommand)
{
    mysqli_set_charset($this->_db, 'utf8');
    $mysqlCommand = new MySqlCommandAdapter($sqlCommand, $this->_db);

    Log::Sql('MySql Query: ' . str_replace('%', '%%', $mysqlCommand->GetQuery()));

    $result = mysqli_query($this->_db, $mysqlCommand->GetQuery());

    $this->_handleError($result);

    return new MySqlReader($result);
}

public function LimitQuery(ISqlCommand $command, $limit, $offset = 0)
{
    return $this->Query(new MySqlLimitCommand($command, $limit, $offset));
}

public function Execute(ISqlCommand $sqlCommand)
{
    mysqli_set_charset($this->_db, 'utf8');
    $mysqlCommand = new MySqlCommandAdapter($sqlCommand, $this->_db);

    Log::Sql('MySql Execute: ' . str_replace('%', '%%', $mysqlCommand->GetQuery()));

    $result = mysqli_query($this->_db, $mysqlCommand->GetQuery());

    $this->_handleError($result);
}

public function GetLastInsertId()
{
    return mysqli_insert_id($this->_db);
}

private function _handleError($result, $sqlCommand = null)
{
    if (!$result)
    {
        if ($sqlCommand != null)
        {
            echo $sqlCommand->GetQuery();
        }
        throw new Exception('There was an error executing your query\n' .  mysql_error());

        Log::Error("Error executing MySQL query %s",  mysql_error());
    }
    return false;
}
}

class MySqlLimitCommand extends SqlCommand
{
/**
 * @var \ISqlCommand
 */
private $baseCommand;

private $limit;
private $offset;

public function __construct(ISqlCommand $baseCommand, $limit, $offset)
{
    parent::__construct();

    $this->baseCommand = $baseCommand;
    $this->limit = $limit;
    $this->offset = $offset;

    $this->Parameters = $baseCommand->Parameters;
}

public function GetQuery()
{
    return $this->baseCommand->GetQuery() . sprintf(" LIMIT %s OFFSET %s",  $this->limit, $this->offset);
}

}
?>

请有人指导我这方面的问题

这很容易,我为您提供了一个经过测试的答案,首先您需要构建数据库 class,如下所示,您需要将其放入一个文件中并从您的主页中包含它(可能是 index.php 页面)。然后,您需要从主页启动您的 class 实例以连接到数据库,代码在这篇很棒的文章中有详细的解释:www.muwakaba.com/php-database-connection

<?php
// Class definition
class Database{

    // The constructor function
    public function __construct(){
        // The properties 
        $this->host = "your_DB_host_address";
        $this->login = "your_DB_login_name";
        $this->password = "your_DB_password";
        $this->name = "your_DB_name";

        // The methods 
        $this->connect(); 
        $this->select();
    }

    // The connect function
    private function connect(){
        $this->connection = mysql_connect($this->host, $this->login, $this->password) or die("Sorry! Cannot connect to the database");
    }

    // The select function
    private function select(){
        mysql_select_db($this->name, $this->connection);
    } 

}
?>