gpt4 book ai didi

PHP OOP while 循环

转载 作者:行者123 更新时间:2023-11-28 23:49:11 25 4
gpt4 key购买 nike

我在使用 OOP 样式从 mysql 循环数据时遇到了问题。

所以,这是我的代码:

class mysql {

private $host;
private $username;
private $password;
private $database;
public $con;

public function connect($setHost, $setUsername, $setPassword, $setDatabase) {

$this->host = $setHost;
$this->username = $setUsername;
$this->password = $setPassword;
$this->database = $setDatabase;

$this->con = mysqli_connect($setHost, $setUsername, $setPassword, $setDatabase);
}

public function query($setQuery) {

$query = mysqli_query($this->con, $setQuery);

return $query;
}

public function fetch($setFetch) {

$fetch = mysqli_fetch_assoc($this->query($setFetch));

return $fetch;
}
}


$objMysql = new mysql();
$con = $objMysql->connect("localhost" , "root" , "root" , "test");

while ($dataSlideshow = $objMysql->fetch("SELECT * FROM slideshow)) {
<h1><?php echo $dataSlideshow['images']; ?></h1>
}

问题是,当我刷新页面时,它在页面中不受限制地执行,我不知道为什么,但是在mysql表中,它只包含三个幻灯片?所以,我的电脑挂了,并强制关闭浏览器。我这里的代码有什么问题?

最佳答案

根据您的函数定义,您的代码将是

$rs=$objMysql->query("SELECT * FROM slideshow");
while ($dataSlideshow = $objMysql->fetch($rs)) {
<h1><?php echo $dataSlideshow['images']; ?></h1>
}

并替换这个函数定义

 public function fetch($rs) {

$row = mysqli_fetch_assoc($rs);

return $row;
}

关于PHP OOP while 循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32920080/

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