PHP代码转换为PHP / MySQLi OOP

时间:2014-07-19 22:18:38

标签: php mysqli

今天我尝试将我的代码转换为PHP / MySQLi OOP代码。

class Database
{
private $host;
private $user;
private $password;
private $db;
private $mysqli;

function __construct()
{
    $this->host = "*****";
    $this->user = "*****";
    $this->password = "******";
    $this->db = "*****";

    $this->mysqli = new mysqli($this->host, $this->user, $this->password, $this->db);

    if (mysqli_connect_errno()):
        printf("Connect failed: %s\n", mysqli_connect_error());
        exit();
    endif;
}
}

这是查询的脚本:

include_once("WD_Config/database.php");

class Adressen_Db
{
function __construct()
{
    $this->database = new Database();
}

public function selecteer()
{
    $query = "SELECT * FROM wd_adressen WHERE verborgen = 0 ORDER BY naam ASC";
    $result = $this->database->mysqli->query($query);

    return $result;
}
}

这就是我所说的。

$adressen = new Adressen_Db;
$adressen_result = $adressen->selecteer();

echo "<p>";
while ($row = $adressen_result->fetch_assoc()):
echo "<a href='http://maps.google.com/?q=".$row['voladres']."'     target='_blank'>".$row['naam']."</a> woonachtig op <i>".$row['voladres']."</i><br>";
endwhile;
echo "</p>";

我总是得到一个“对非对象的成员函数查询()的调用”。不管我是什么......

有人可以告诉我为什么会这样吗?

谢谢!

3 个答案:

答案 0 :(得分:5)

类Database中的$mysqli变量声明为private

您只能通过setter和getter访问它。

答案 1 :(得分:0)

我认为虽然您肯定需要将$mysqli公开,以便可以在其他方法中访问,但可能还有其他内容,因为错误类似于

  

尝试访问数据库类中的私有属性

或类似的东西,而你的脚本抛出非对象调用错误

我认为你的new Adressen_Db;缺少括号:

$adressen = new Adressen_Db();

答案 2 :(得分:0)

您可以使用以下代码替换代码:
Config.php

gameBids.aggregate([
        { 
            $group : { 
                _id: "$userId",
                countBids           : { $sum : 1 },
                winCount            : { $sum : { $cond: { }}},
                loseCount           : { $sum : { $cond: { }}},
                pendingCount        : { $sum : { $cond: { }}},
                sumbiddingPoints    : { $sum: '$biddingPoints'},
                sumWinPoint         : { $sum: '$gameWinPoints'}
            }
        }
    ], function (error, group) {
            if (error) throw error;
            else res.json(group);
    });

现在将此文件包含在数据库文件中

<?php

define("DB_HOST", "localhost");
define("DB_USER", "root");
define("DB_PASS", "");
define("DB_NAME", "your_database_name");

现在,按以下方式进行查询:

require_once 'config.php';

Class Database {

    public $host = DB_HOST;
    public $user = DB_USER;
    public $pass = DB_PASS;
    public $dbname = DB_NAME;
    public $link;
    public $error;

    public function __construct() {
        $this->getConnection();
    }

    private function getConnection() {
        $this->link = new mysqli($this->host, $this->user, $this->pass, $this->dbname);
        if (!$this->link) {
            $this->error = "Connection failed" . $this->link->connect_error;
            return false;
        }
    }

    // for only select query
    public function select($query) {
        $result = $this->link->query($query) or
                die($this->link->error . __LINE__);
        if ($result->num_rows > 0) {
            return $result;
        } else {
            return false;
        }
    }

    // for insert, delete and update
    public function myquery($query) {
        $myquery = $this->link->query($query) or
                die($this->link->error . __LINE__);
        if ($myquery) {
            return $myquery;
        } else {
            return false;
        }
    }

}

执行此操作。