gpt4 book ai didi

php - OOP PHP undefined variable 和空查询

转载 作者:行者123 更新时间:2023-11-29 12:50:15 25 4
gpt4 key购买 nike

我正在尝试通过查询测试数据库来学习 OOP PHP。我尝试用谷歌搜索答案,但没有成功。这是我的代码:

<?php

class DB {

protected $db;
protected $query;

public function __construct() {
$this->db = new mysqli("localhost","root","","test2");
$this->query = "SELECT * FROM test";
}


public function querydb() {
$this->db->query($db,$query);
}

}

$database = new DB();
$database->querydb();


?>

它表示构造函数中定义的两个变量未定义,并且查询为空。有什么建议吗?

最佳答案

更改:

$this->db->query($db,$query);

致:

$this->db->query($this->query);

试试这个:

<?php

class DB {

protected $db;
protected $query;

public function __construct() {
$this->db = new mysqli("localhost","root","","test2");
$this->query = "SELECT * FROM test";
}


public function querydb() {
$this->db->query($this->query);
}

}

$database = new DB();
$database->querydb();


?>

关于php - OOP PHP undefined variable 和空查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24863955/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com