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. 使用Python 3更新MySQL数据库
  2. 使用IP地址方法登录MySQL数据库Can&#39;t connect to MySQL serv
  3. 关于淘宝的数据库系统
  4. PHP读取Excel文件的内容并写入Mysql数据库
  5. MySQL数据库引擎简介
  6. PHP数据库类的封装
  7. python pymysql连接数据库pymysql.err.OperationalError 1044
  8. Log4j2记录日志到数据库(MySQL&MongoDB)
  9. MYSQL5.5和5.6参数的差异

随机推荐

  1. 25K的996 和 18K的965,你选哪个?
  2. 一文讲透微服务下如何保证事务的一致性
  3. 你的影响力,往往被忽视
  4. 队列(静态方式)
  5. 病毒与故障:漫谈计算机软件的故障应对
  6. 最全的DevOps工具集合,再也不怕选型了!
  7. 如何优雅地运用位运算实现产品需求
  8. 近几年,你也没干出满意的成绩吗?
  9. 如何优雅地停止一个线程?
  10. 再来一次高考