【问题标题】:Issue accessing $dbconnect within OO PHP mysqli select method在 OO PHP mysqli 选择方法中访问 $dbconnect 的问题
【发布时间】:2016-10-21 02:37:54
【问题描述】:

我正在尝试创建一个 OO PHP 数据库类,但在解决如何将我的 $dbconnect 变量传递给“选择”方法时遇到问题。下面是我的代码:

class Database {

private $db_host = "host";
private $db_user = "user";
private $db_pass = "password";
private $db_name = "dbname";
private $result = array();
private $myQuery = "";
private $numResults = "";

public function connect() {

    $dbconnect = mysqli_connect($this->db_host, $this->db_user, $this->db_pass, $this->db_name);
    if(mysqli_connect_errno()) {

        array_push($this->result, mysqli_connect_error());
        return false;

    } else {

        $this->con = true;
        return true;

    }

}

public function select($table, $rows = '*', $join = null, $where = null, $order = null, $limit = null) {

    $sql = 'SELECT '.$rows;
    if($table) {
        $sql .= ' FROM '.$table;
    }
    if($join != null) {
        $sql .= ' JOIN '.$join;
    }
    if($where != null) {
        $sql .= ' WHERE '.$where;
    }
    if($order != null) {
        $sql .= ' ORDER BY '.$order;
    }
    if($limit != null) {
        $sql .= ' LIMIT '.$limit;
    }

    $query = $dbconnect->query($sql);
    $this->myQuery = $sql;

    if($query->num_rows > 0) {

        $this->numResults = $query->num_rows;

        for($i = 0; $i < $this->numResults; $i++) {

            $r = mysqli_fetch_array($query);
            $key = array_keys($r);
            for($x = 0; $x < count($key); $x++) {

                if(!is_int($key[$x])) {
                    if(mysqli_num_rows($query) >= 1) {
                        $this->result[$i][$key[$x]] = $r[$key[$x]];
                    } else {
                        $this->result = null;
                    }
                }

            }

        }
        return true;

    } else {

        //array_push($this->selectresult, 'No rows returned');
        return false;

    }

}

所以我有一个方法来创建数据库连接,它将它分配给 $dbconnect 变量。我的问题是,当我使用 mysqli 时,我需要能够在我的 select 方法中获取这个变量......但不能用上面的代码这样做。我收到以下错误:

注意:未定义变量:dbconnect

任何指出我哪里出错的帮助都会很棒。

【问题讨论】:

  • 1) 这个$this-&gt;con = true; 是错误的,我在你的班级中没有看到任何实例成员$con。 2)您在下面得到了两个答案,两者都应该适合您。

标签: php mysqli


【解决方案1】:

将其声明为类属性

private $dbconnect;

然后在你的构造函数中,初始化它

$this->dbconnect = mysqli_connect($this->db_host, $this->db_user, $this->db_pass, $this->db_name);

现在您可以在整个班级中将其引用为$this-&gt;dbconnect

【讨论】:

    【解决方案2】:

    你可以创建这个类的属性

    protected $dbconnect;
    

    然后创建一个构造函数来设置$dbconnect的值

    public function __construct()
    {
         $this->dbconnect = mysqli_connect($this->db_host, $this->db_user, $this->db_pass, $this->db_name);
    }
    

    然后在你的 select() 函数中使用

    $query = $this-&gt;dbconnect-&gt;query($sql); 而不是$query = $dbconnect-&gt;query($sql);

    希望这会有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-12-04
      • 2015-04-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-01-28
      • 1970-01-01
      • 2010-11-30
      相关资源
      最近更新 更多