【问题标题】:Need some help debugging this PHP需要一些帮助调试这个 PHP
【发布时间】:2013-04-06 06:00:29
【问题描述】:

我之前编写了这个代码,但它不起作用。

我意识到我可以更简单地做到这一点,但我正在尝试使用 OOP。

所以这里是代码..

database.class.php

<?php

class config {

    public $host;
    public $user;
    public $pass;

    function __construct($host=NULL,$user=NULL,$pass=NULL) {

        $this->host = $host;
        $this->user = $user;
        $this->pass = $pass;

    }

    function __destruct() {}

}

class database {

    private $host;
    private $user;
    private $pass;
    private $config;

    function __construct($config) {
        $this->config = $config;
    }

    function __destruct() {}

    public function openCon() {
        mysql_connect($this->host,$this->user,$this->pass);
        if (mysql_errno()) {
            printf('Failed to connect: %s', mysql_error());
        } else {
            echo 'Connected to DB successfully.<br/><br/>';
        }
    }

    public function closeCon() {
        try {
            mysql_close();
            echo 'Successfully closed connection.<br/><br/>';
        } catch (exception $e) {
            echo $e;
        }
    }
}

?>

index.php

<?php
    include('db.class.php');

    $con = new config('localhost','root','');
    $etc = new database($con);
    $etc->openCon();
    mysql_select_db('tester') or die(mysql_error());
    $query1 = mysql_query('SELECT * FROM person') or die(mysql_error());
    $rows = mysql_fetch_array($query1) or die(mysql_error());
        echo 'First: ' . $rows['First'];
        echo 'Age:'    . $rows['Age'];
    $etc->closeCon();
?>

我确定这其中存在明显的错误。谁能帮我找到并修复它们?

它说:用户''@'localhost'拒绝访问数据库'tester'

不知道为什么。

我将 root 指定为用户,但它返回 '' 作为我请求使用的用户。

它得到了正确的主机和数据库。

我的本​​地 root 用户没有密码,所以...

有什么想法吗?

【问题讨论】:

  • Access denied 错误表示您没有提供正确的登录信息。先解决这个问题。
  • @SverriM.Olsen 就是这样。这是正确的。我以前用这些详细信息登录过。

标签: mysql database php


【解决方案1】:

好的,问题是从未设置过主机、用户和密码。查看database 构造函数。您只需设置配置。

要么以某种方式设置私有database 属性,要么使用openCon() 中的配置对象。

或者,更好的是,直接废弃 config 对象。目前,它没有发挥重要作用。只需像下面这样更改database 构造函数并完成它:

function __construct($host, $user, $pass) {
    $this->host = $host;
    $this->user = $user;
    $this->pass = $pass;
}

最后,如果__destruct() 方法没有做任何事情,那么你不需要包含它。

【讨论】:

    【解决方案2】:

    您的一切都是正确的,只是您需要更改数据库类。您将 $config 作为对象传递给数据库类的构造函数,因此要从该对象访问变量,您几乎不需要更改。应该是

     class database {
    
    private $host;
    private $user;
    private $pass;
    private $config;
    public $Connectionlink;
    
    function __construct($config) {
        $this->host = $config->host;
        $this->user = $config->user;
        $this->pass = $config->pass;
    }
    
     // function __destruct() {} as your destruct function doing noting so you need not to include this.
    
    public function openCon() {
       $this->Connectionlink= mysql_connect($this->host,$this->user,$this->pass);
        if (mysql_errno()) {
            printf('Failed to connect: %s', mysql_error());
        } else {
            echo 'Connected to DB successfully.<br/><br/>';
        }
    }
    
    public function closeCon() {
        try {
            mysql_close();
            echo 'Successfully closed connection.<br/><br/>';
        } catch (exception $e) {
            echo $e;
        }
    }
    }
    

    mysql_select_db需要两个参数,所以应该是

      mysql_select_db('tester', $etc->Connectionlink) or die(mysql_error());
    

    【讨论】:

      【解决方案3】:

      尝试使用 cmd 导航到您的 mysql 目录 在 xampp 它的“C:\xampp\mysql\bin”

      然后输入 “mysql -u root”并检查您是否正在访问

      【讨论】:

        猜你喜欢
        • 2019-05-12
        • 1970-01-01
        • 1970-01-01
        • 2016-02-10
        • 1970-01-01
        • 2011-01-13
        • 1970-01-01
        • 2020-08-06
        • 1970-01-01
        相关资源
        最近更新 更多