我正在尝试使用PDO从数据库中显示表值,但在执行此操作时显示错误
“致命错误:调用未定义的方法ManageUsers :: fetchAll()”
请提供解决方案来解决这个问题?
列表seller.php
<?php
include "C:/wamp/www/Super_Admin_MangoAir/classes/class.ManageUsers.php";
$sellers = new ManageUsers();
//$sellers->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
while ($row = $sellers->fetchAll(PDO::FETCH_ASSOC)) {
?>
<tr>
<td><?php echo $row['company_name']; ?>
<span class="pull-right-container">
<small class="label pull-center bg-green">Premium Member</small>
</span>
</td>
<td><?php echo $row['email']; ?></td>
<td><?php echo $row['mobile']; ?></td>
<td><?php echo $row['count_free']; ?></td>
<td><?php echo $row['count_travel']; ?>
<span class="pull-right-container">
<small class="label pull-center bg-green">Paid</small>
</span>
</td>
<td><a href=""><button type="button" class="btn btn-flat btn-default btn-sm disabled">Already Paid</button></a></td>
</tr>
<?php } ?>
class.ManageUsers.php
<?php
class ManageUsers{
include "C:/wamp/www/Super_Admin_MangoAir/classes/class.database.php";
public $link;
function __construct(){
$db_connection = new dbConnection();
$this->link = $db_connection->connect();
return $this->link;
}
function seller_list($result){
$result = $this->link->prepare("SELECT company_name,email,phone,count_free,count_travel FROM login");
$result->execute();
}
}
?>
class.database.php
<?php
class dbConnection{
protected $db_conn;
public $db_name='company';
public $db_user='root';
public $db_pass='';
public $db_host='localhost';
function connect (){
try{
$this->db_conn=new PDO("mysql:host=$this->db_host;dbname=$this->db_name",$this->db_user,
$this->db_pass);
return $this->db_conn;
}
catch(PDOException $e){
return $e->getMessage();
echo 'errorrrrrrrr';
}
}
}
?>
答案 0 :(得分:0)
您需要添加一个显示表中数据的SQL查询。尝试在$sellers = new ManageUsers();
之后在list-seller.php中添加这些行:
$sql = 'SHOW TABLES';
$sellers->query($sql);
while ($row = $sellers->fetchAll(PDO::FETCH_ASSOC))
{
答案 1 :(得分:0)
给定类的构造函数只能返回所述类的实例,没有其他返回值。 见Constructor returning value?
我建议你改为上课:
class ManageUsers
{
include "C:/wamp/www/Super_Admin_MangoAir/classes/class.database.php";
private $link;
public function __construct()
{
$db_connection=new dbConnection();
$this->link=$db_connection->connect();
}
public function getLink()
{
return $this->link;
}
并使用最后一个功能。
//list-seller.php//
<?php
include "C:/wamp/www/Super_Admin_MangoAir/classes/class.ManageUsers.php";
$manager = new ManageUsers();
$sellers = $manager->getLink();
答案 2 :(得分:0)
尝试从构造函数中删除return $this->link
并使用$sellers->link->fetchAll(PDO::FETCH_ASSOC)
答案 3 :(得分:-1)
<强> 更改 强>
1)从班级管理员(内部)中删除include "C:/wamp/www/Super_Admin_MangoAir/classes/class.database.php";
并将其放在上面。
2)在seller_list()
之前公开。
3)在 list-seller.php 页面中致电seller_list()
。
4)在功能return
中错过seller_list()
个关键字。添加它。
5)从$result
中的seller_list()
功能移除Class ManageUsers
,因为它没用。
更新代码
<强>列表seller.php 强>
<?php
include "C:/wamp/www/Super_Admin_MangoAir/classes/class.ManageUsers.php";
$sellers = new ManageUsers();
$query = $sellers->seller_list();
foreach($query as $row){
?>
<tr>
<td><?php echo $row['company_name']; ?>
<span class="pull-right-container">
<small class="label pull-center bg-green">Premium Member</small>
</span>
</td>
<td><?php echo $row['email']; ?></td>
<td><?php echo $row['mobile']; ?></td>
<td><?php echo $row['count_free']; ?></td>
<td><?php echo $row['count_travel']; ?>
<span class="pull-right-container">
<small class="label pull-center bg-green">Paid</small>
</span>
</td>
<td><a href=""><button type="button" class="btn btn-flat btn-default btn-sm disabled">Already Paid</button></a></td>
</tr>
<?php } ?>
<强> class.ManageUsers.php 强>
<?php
include "C:/wamp/www/Super_Admin_MangoAir/classes/class.database.php";
class ManageUsers{
public $link;
function __construct(){
$db_connection = new dbConnection();
$this->link = $db_connection->connect();
}
public function getDB(){
return $this->link;
}
public function seller_list(){
$result = $this->getDB()->prepare("SELECT company_name,email,phone,count_free,count_travel FROM login");
$result->execute();
return $result->fetchAll();
}
}
?>