gpt4 book ai didi

php - 使用Php mysql查询不从php类获取数据,尽管有数据

转载 作者:行者123 更新时间:2023-11-29 11:40:01 26 4
gpt4 key购买 nike

class book {

function __construct() {
$conn = mysql_connect('localhost', 'root', '') or die('can nit connect to DB');
mysql_select_db('atomic_project', $conn) or die('can not connect to db');
}

public function listView() {
$allData = array();
$query = "SELECT * FROM `book`";
$result = mysql_query($query);
while ($row = mysql_fetch_assoc($result)) {
print_r($row);
$allData [] = $row;
}
return $allData;
}

}

我的索引页

    $listViewObj = new book();
$allData = $listViewObj->listView();

echo "<pre>";
print_r($allData);
echo "<pre>";

这是我的代码和表格 here is my table data ,我可以将数据插入表中,但没有找到行我不明白为什么没有显示数据,请帮助我

enter image description here

最佳答案

    <?php
class book {

public function listView() {
$conn = mysql_connect('localhost', 'root', '') or die('can nit connect to DB');
mysql_select_db('atomic_project', $conn) or die('can not connect to db');

$allData = array();
$query = "SELECT * FROM `book`";
$result = mysql_query($query);
while ($row = mysql_fetch_assoc($result)) {
//echo "<pre>";
//print_r($row);
$allData [] = $row;
}
return $allData;
}
}
book::listView(); //scope resolution operator
?>

或通过对象调用函数

$obj = new book();
$obj->listView();

关于php - 使用Php mysql查询不从php类获取数据,尽管有数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35957173/

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