【问题标题】:How to show table values in php using PDO [duplicate]如何使用 PDO 在 php 中显示表值
【发布时间】:2017-03-04 12:14:45
【问题描述】:

我正在尝试使用 PDO 显示数据库中的表值,但在执行此操作时会显示 错误

“致命错误:调用未定义的方法 ManageUsers::fetchAll()”

请提供解决方案?

list-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']; ?>&nbsp;
      <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';

                        }
                }

    }


?>

【问题讨论】:

  • 好吧,ManageUsers 中没有 fetchAll

标签: php mysql pdo


【解决方案1】:

变化

1) 从类 ManageUsers(内部)中删除 include "C:/wamp/www/Super_Admin_MangoAir/classes/class.database.php"; 并将其放在上面。

2)seller_list()之前添加public。

3)list-seller.php 页面中调用seller_list()

4) 函数seller_list() 中缺少return 关键字。添加它。

5)Class ManageUsers 中的seller_list() 函数中删除$result,因为它没有用。

更新代码

list-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']; ?>&nbsp;
      <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();
  }
}
?>

【讨论】:

  • 致命错误:出现非对象错误调用成员函数fetchAll()
  • Still Not Working 与“致命错误:在出现非对象错误时调用成员函数 fetchAll()”相同的错误:(@Nana Partykar
  • 将您的数据库连接文件粘贴到您的问题@user1
  • 谢谢兄弟!它有效!!!:)
  • 谁投反对票请给出正当理由。既然你不喜欢我的答案或者可能有任何缺陷,所以,请 GUTS 提供正确的答案。 不要简单地拒绝投票和逃避。我会尽我最大的努力帮助用户。甚至 OP @user1 问题也得到了解决。然后,我认为没有人对此有任何问题。 ~ 恶心的行为
【解决方案2】:

您需要添加一个显示表中数据的 sql 查询。尝试在$sellers = new ManageUsers();之后的list-seller.php中添加这些行:

$sql = 'SHOW TABLES';
$sellers->query($sql);
while ($row = $sellers->fetchAll(PDO::FETCH_ASSOC))
                    {

【讨论】:

  • 我已经在 class.ManageUsers.php 的 $result 中添加了查询...所以我怎样才能将这个 ($result) 放入 list-seller.php
  • nlo,在seller.php中添加这个
【解决方案3】:

给定类的构造函数只能返回该类的实例,不能返回其他值。 见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();

【讨论】:

    【解决方案4】:

    尝试从您的构造函数中删除 return $this-&gt;link 并使用 $sellers-&gt;link-&gt;fetchAll(PDO::FETCH_ASSOC) 调用 fetchAll

    【讨论】:

    • 致命错误:调用未定义的方法 PDO::fetchAll()....
    猜你喜欢
    • 2011-04-09
    • 1970-01-01
    • 1970-01-01
    • 2018-01-24
    • 2017-02-09
    • 1970-01-01
    • 2017-09-21
    • 2023-01-03
    相关资源
    最近更新 更多