无法使用php连接到mysql

时间:2016-01-24 08:07:25

标签: php mysql database mysqli windows-server-2012-r2

尝试通过PHP连接时显示

警告:mysqli_connect():( HY000 / 1044):用户''@ localhost'拒绝访问C:\ wamp \ www \ booked \ lib \ Database \ MySQL \ MySqlConnection.php中的数据库'bookedscheduler'第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中给出

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

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

}
?>

请有人在这方面指导我

1 个答案:

答案 0 :(得分:0)

这很简单,我为您提供了经过测试的答案,首先您需要构建如下所示的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);
    } 

}
?>