Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/272.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/sql-server-2008/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
OOP-PHP集成mysqli\u查询_Php_Mysql_Oop_Mysqli_Integrate - Fatal编程技术网

OOP-PHP集成mysqli\u查询

OOP-PHP集成mysqli\u查询,php,mysql,oop,mysqli,integrate,Php,Mysql,Oop,Mysqli,Integrate,我将编辑这个问题使它更清楚,这样你就可以看到我现在得到了什么,并且更容易理解这个问题 <?php $mysqli = new mysqli("localhost", "user", "password", "test"); class building { private $mysqli; public $buildingid; public $userid; public $buildinglevel; public function __construct($buildingi

我将编辑这个问题使它更清楚,这样你就可以看到我现在得到了什么,并且更容易理解这个问题

<?php
$mysqli = new mysqli("localhost", "user", "password", "test");
class building
{
private $mysqli;
public $buildingid;
public $userid;
public $buildinglevel;




public function __construct($buildingid, $userid, \mysqli $mysqli)
{
    $this->buildinglevel;
    $this->mysqli = $mysqli;
}

public function getLevel()
{
    return $this->mysqli->query("SELECT ".$this->buildingid." FROM worlds WHERE city_userid=".$this->userid."");
}
}

}
?>

结果是空的,什么也没有发生。

您应该将mysqli的实例注入到building类的_construct():

$mysqli=newmysqli('user','password','localhost','test')

如果($mysqli->connect\u errno){ printf(“连接失败:%s\n”,$mysqli->Connect\u错误); }


您应该将mysqli的实例注入到building类的_construct():

$mysqli=newmysqli('user','password','localhost','test')

如果($mysqli->connect\u errno){ printf(“连接失败:%s\n”,$mysqli->Connect\u错误); }


您应该将mysqli的实例注入到building类的_construct():

$mysqli=newmysqli('user','password','localhost','test')

如果($mysqli->connect\u errno){ printf(“连接失败:%s\n”,$mysqli->Connect\u错误); }


您应该将mysqli的实例注入到building类的_construct():

$mysqli=newmysqli('user','password','localhost','test')

如果($mysqli->connect\u errno){ printf(“连接失败:%s\n”,$mysqli->Connect\u错误); }


你的类看起来是一个模型:它代表某种形式的数据,在你的例子中是一个特定的建筑

一种方法是将MySQLi对象作为构造函数对象传递,以及要查询的建筑ID,并将结果分配给类属性。这将如下所示:

<?php
class Building
{
    private $db;

    protected $id;
    protected $level;

    public function __construct(mysqli $db, $id = null)
    {
        $this->db = $db;

        if (!is_null($id) && intval($id) > 0) {
            $stmt = $this->db->prepare("SELECT buildingid, buildinglevel FROM building WHERE `id` = ?");
            $stmt->bind_param('i', $id);
            $stmt->execute();
            $stmt->bind_result($this->id, $this->level);
            $stmt->fetch();
        }
    }

    public function getId()
    {
        return (int)$this->id;
    }

    public function getLevel()
    {
        return (int)$this->level;
    }
}

你的类看起来是一个模型:它代表某种形式的数据,在你的例子中是一个特定的建筑

一种方法是将MySQLi对象作为构造函数对象传递,以及要查询的建筑ID,并将结果分配给类属性。这将如下所示:

<?php
class Building
{
    private $db;

    protected $id;
    protected $level;

    public function __construct(mysqli $db, $id = null)
    {
        $this->db = $db;

        if (!is_null($id) && intval($id) > 0) {
            $stmt = $this->db->prepare("SELECT buildingid, buildinglevel FROM building WHERE `id` = ?");
            $stmt->bind_param('i', $id);
            $stmt->execute();
            $stmt->bind_result($this->id, $this->level);
            $stmt->fetch();
        }
    }

    public function getId()
    {
        return (int)$this->id;
    }

    public function getLevel()
    {
        return (int)$this->level;
    }
}

你的类看起来是一个模型:它代表某种形式的数据,在你的例子中是一个特定的建筑

一种方法是将MySQLi对象作为构造函数对象传递,以及要查询的建筑ID,并将结果分配给类属性。这将如下所示:

<?php
class Building
{
    private $db;

    protected $id;
    protected $level;

    public function __construct(mysqli $db, $id = null)
    {
        $this->db = $db;

        if (!is_null($id) && intval($id) > 0) {
            $stmt = $this->db->prepare("SELECT buildingid, buildinglevel FROM building WHERE `id` = ?");
            $stmt->bind_param('i', $id);
            $stmt->execute();
            $stmt->bind_result($this->id, $this->level);
            $stmt->fetch();
        }
    }

    public function getId()
    {
        return (int)$this->id;
    }

    public function getLevel()
    {
        return (int)$this->level;
    }
}

你的类看起来是一个模型:它代表某种形式的数据,在你的例子中是一个特定的建筑

一种方法是将MySQLi对象作为构造函数对象传递,以及要查询的建筑ID,并将结果分配给类属性。这将如下所示:

<?php
class Building
{
    private $db;

    protected $id;
    protected $level;

    public function __construct(mysqli $db, $id = null)
    {
        $this->db = $db;

        if (!is_null($id) && intval($id) > 0) {
            $stmt = $this->db->prepare("SELECT buildingid, buildinglevel FROM building WHERE `id` = ?");
            $stmt->bind_param('i', $id);
            $stmt->execute();
            $stmt->bind_result($this->id, $this->level);
            $stmt->fetch();
        }
    }

    public function getId()
    {
        return (int)$this->id;
    }

    public function getLevel()
    {
        return (int)$this->level;
    }
}

对象是封装通过方法向其他对象公开的行为的单元。从数据库中检索到的公共属性的包装器不能用于面向对象编程。事实上,
mysqli
可以通过
fetch\u object
为您做到这一点:

$result = $mysqli->query($query);
while ($building = $result->fetch_object()) {
    // access properties via $building->buildingid, etc.
}
除非
building
类实际上通过方法提供功能,并实现一些抽象,否则它是不需要的。取而代之的是一个DAO(数据访问对象),它包装DB(
mysqli
),它检索的数据由您的模型使用

interface Dao {
    public function get($id);
}

class BuildingDao implements Dao {
    // DB can be a wrapper for mysqli
    // but servers as an interface so it
    // can be replaced easily
    public function __construct(DB $db) {
        $this->db = $db;
    }

    public function get($id) {
        return $this->db->prepare(
            "SELECT buildinglevel FROM building WHERE buildingid = ?"
        )->execute($id)->fetch();
    }
}

对象是封装通过方法向其他对象公开的行为的单元。从数据库中检索到的公共属性的包装器不能用于面向对象编程。事实上,
mysqli
可以通过
fetch\u object
为您做到这一点:

$result = $mysqli->query($query);
while ($building = $result->fetch_object()) {
    // access properties via $building->buildingid, etc.
}
除非
building
类实际上通过方法提供功能,并实现一些抽象,否则它是不需要的。取而代之的是一个DAO(数据访问对象),它包装DB(
mysqli
),它检索的数据由您的模型使用

interface Dao {
    public function get($id);
}

class BuildingDao implements Dao {
    // DB can be a wrapper for mysqli
    // but servers as an interface so it
    // can be replaced easily
    public function __construct(DB $db) {
        $this->db = $db;
    }

    public function get($id) {
        return $this->db->prepare(
            "SELECT buildinglevel FROM building WHERE buildingid = ?"
        )->execute($id)->fetch();
    }
}

对象是封装通过方法向其他对象公开的行为的单元。从数据库中检索到的公共属性的包装器不能用于面向对象编程。事实上,
mysqli
可以通过
fetch\u object
为您做到这一点:

$result = $mysqli->query($query);
while ($building = $result->fetch_object()) {
    // access properties via $building->buildingid, etc.
}
除非
building
类实际上通过方法提供功能,并实现一些抽象,否则它是不需要的。取而代之的是一个DAO(数据访问对象),它包装DB(
mysqli
),它检索的数据由您的模型使用

interface Dao {
    public function get($id);
}

class BuildingDao implements Dao {
    // DB can be a wrapper for mysqli
    // but servers as an interface so it
    // can be replaced easily
    public function __construct(DB $db) {
        $this->db = $db;
    }

    public function get($id) {
        return $this->db->prepare(
            "SELECT buildinglevel FROM building WHERE buildingid = ?"
        )->execute($id)->fetch();
    }
}

对象是封装通过方法向其他对象公开的行为的单元。从数据库中检索到的公共属性的包装器不能用于面向对象编程。事实上,
mysqli
可以通过
fetch\u object
为您做到这一点:

$result = $mysqli->query($query);
while ($building = $result->fetch_object()) {
    // access properties via $building->buildingid, etc.
}
除非
building
类实际上通过方法提供功能,并实现一些抽象,否则它是不需要的。取而代之的是一个DAO(数据访问对象),它包装DB(
mysqli
),它检索的数据由您的模型使用

interface Dao {
    public function get($id);
}

class BuildingDao implements Dao {
    // DB can be a wrapper for mysqli
    // but servers as an interface so it
    // can be replaced easily
    public function __construct(DB $db) {
        $this->db = $db;
    }

    public function get($id) {
        return $this->db->prepare(
            "SELECT buildinglevel FROM building WHERE buildingid = ?"
        )->execute($id)->fetch();
    }
}

在构造函数中执行它,这就是它的用途。我试图将它添加到构造函数中,但我不想每次都重新定义$mysqli的值,这似乎是浪费时间?因此,基本上,我如何将$mysqli保持在类之外,并导入它,以便能够在构造函数内部执行查询?询问代码的问题应该包括尝试的解决方案,这些解决方案做了什么,以及您预期会发生什么。(又名“必须有人对某个问题有最低限度的理解才能就此提出问题”)@Narf虽然你可以在构造函数中进行计算,但这是一个让单元测试几乎不可能的可怕想法。构造函数是用来初始化类状态的,基本上(即注入适当的依赖项)是在构造函数中进行的,这就是它的用途。我试图将它添加到构造函数中,但我不想每次都重新定义$mysqli的值,这似乎是浪费时间?那么,基本上,我如何将$mysqli保持在类之外,并导入它,以便能够在构造函数内部执行查询呢