无法使用php连接到mysql

时间:2023-02-03 18:15:32

While trying to connect through PHP it displays

在尝试通过PHP连接时,它会显示出来

Warning: mysqli_connect(): (HY000/1044): Access denied for user ''@'localhost' to database 'bookedscheduler' in C:\wamp\www\booked\lib\Database\MySQL\MySqlConnection.php on line 52

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

Warning: mysqli_select_db() expects parameter 1 to be mysqli, boolean given in C:\wamp\www\booked\lib\Database\MySQL\MySqlConnection.php on line 53

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

Warning: mysqli_set_charset() expects parameter 1 to be mysqli, boolean given in C:\wamp\www\booked\lib\Database\MySQL\MySqlConnection.php on line 54

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

Cannot modify header information - headers already sent by (output started at C:\wamp\www\booked\lib\Database\MySQL\MySqlConnection.php:53) in C:\wamp\www\booked\Pages\Page.php on line 138

无法修改标头信息 - 已在C:\ wamp \ www \ booked \ Pages \ Page.php中发送的标头(输出从C:\ wamp \ www \ booked \ lib \ Database \ MySQL \ MySqlConnection.php:53开始)第138行

My config.php database connection

我的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 file

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

}
?>

Please someone guide me in this regard

请有人在这方面指导我

1 个解决方案

#1


0  

It is very easy and I am providing you a tested answer, first you need to build the Database class as seen below, you need to put it in a file and include it from your homepage (might be the index.php page). Then, you need to initiate an instance of your class from the homepage to connect to the database, the code is very well explained in details in this great article: www.muwakaba.com/php-database-connection

这很简单,我为您提供了经过测试的答案,首先您需要构建如下所示的Database类,您需要将其放在一个文件中,并将其包含在您的主页中(可能是index.php页面)。然后,您需要从主页启动您的类的实例以连接到数据库,在这篇伟大的文章中详细解释了代码: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);
    } 

}
?>

#1


0  

It is very easy and I am providing you a tested answer, first you need to build the Database class as seen below, you need to put it in a file and include it from your homepage (might be the index.php page). Then, you need to initiate an instance of your class from the homepage to connect to the database, the code is very well explained in details in this great article: www.muwakaba.com/php-database-connection

这很简单,我为您提供了经过测试的答案,首先您需要构建如下所示的Database类,您需要将其放在一个文件中,并将其包含在您的主页中(可能是index.php页面)。然后,您需要从主页启动您的类的实例以连接到数据库,在这篇伟大的文章中详细解释了代码: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);
    } 

}
?>