简体   繁体   English

需要一些调试此PHP的帮助

[英]Need some help debugging this PHP

I coded this up a bit ago, but it's not working. 我之前对此进行了编码,但无法正常工作。

I realize I could do this simpler, but I'm trying to use OOP. 我意识到我可以做得更简单,但是我正在尝试使用OOP。

So here is the code.. 所以这是代码。

database.class.php 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 的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();
?>

I'm sure there are blaring errors in this. 我敢肯定这有错误。 Could anyone help me find and fix them? 谁能帮我找到并修复它们?

It says: Access denied for user ''@'localhost' to database 'tester' 它说:拒绝用户“ @” localhost”访问数据库“ tester”

Not sure why. 不知道为什么。

I specified root as the user, but it came back with '' as the user I requested to use. 我将root指定为用户,但使用”作为我要求使用的用户回来。

It got the host and the database right. 它使主机和数据库正确。

There is no password to my local root user, so... 我的本地root用户没有密码,所以...

Any ideas? 有任何想法吗?

Okay, the problem is that the host, user and password are never set. 好的,问题是主机,用户和密码从未设置过。 Look in the database constructor. database构造函数中查找。 You only set the configuration. 您仅设置配置。

Either set the private database properties somehow, or use the configuration object in openCon() . 要么以某种方式设置私有database属性,要么在openCon()使用配置对象。

Or, even better, just scrap the config object. 或者,甚至更好,只需废弃config对象。 Right now it plays no significant role. 目前,它没有任何重要作用。 Just change the database constructor like the following and be done with it: 只需像下面那样更改database构造函数并完成它:

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

Finally, if the __destruct() method does not do anything then you do not need to include it. 最后,如果__destruct()方法不执行任何操作,则不需要包含它。

Your every thing is correct just you need to change in your database class. 您的每件事都是正确的,只是您需要更改数据库类。 You pass $config as an object to the constructor of the database class so to access the variable from that object you need little change. 您将$ config作为对象传递给数据库类的构造函数,因此只需很少更改即可从该对象访问变量。 It should be 它应该是

 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 need two parameters, so it should be mysql_select_db需要两个参数,因此应该

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

just try to navigate to your mysql directory using cmd in xampp its "C:\\xampp\\mysql\\bin" 只需尝试在xampp中使用cmd导航到您的mysql目录,其“ C:\\ xampp \\ mysql \\ bin”

and then type "mysql -u root" and check whether you are getting access 然后键入“ mysql -u root”,并检查您是否正在访问

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM