簡體   English   中英

需要一些調試此PHP的幫助

[英]Need some help debugging this PHP

我之前對此進行了編碼,但無法正常工作。

我意識到我可以做得更簡單,但是我正在嘗試使用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用戶沒有密碼,所以...

有任何想法嗎?

好的,問題是主機,用戶和密碼從未設置過。 database構造函數中查找。 您僅設置配置。

要么以某種方式設置私有database屬性,要么在openCon()使用配置對象。

或者,甚至更好,只需廢棄config對象。 目前,它沒有任何重要作用。 只需像下面那樣更改database構造函數並完成它:

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

最后,如果__destruct()方法不執行任何操作,則不需要包含它。

您的每件事都是正確的,只是您需要更改數據庫類。 您將$ 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());

只需嘗試在xampp中使用cmd導航到您的mysql目錄,其“ C:\\ xampp \\ mysql \\ bin”

然后鍵入“ mysql -u root”,並檢查您是否正在訪問

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM