2016-10-22 48 views
0

我嘗試使用PDO來顯示從數據庫表中的值顯示在PHP表中的值,但此時它顯示了一個錯誤如何使用PDO

"Fatal error: Call to undefined method 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']; ?>&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'; 

         } 
       } 

    } 


?> 
+0

從構造函數中刪除return $this->link,並呼籲使用fetchall以及存在'ManageUsers' – Drew

回答

-1

變化

1)卸下類ManageUsers(內部)include "C:/wamp/www/Super_Admin_MangoAir/classes/class.database.php";並把它上面。

2)seller_list()之前公開。

3)呼叫seller_list()列表seller.php頁。

4)錯過return關鍵字在功能seller_list()。添加它。

5)seller_list()功能Class ManageUsers刪除$result,因爲它是沒有用的。

更新的代碼

列表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(); 
    } 
} 
?> 
+0

致命錯誤:調用成員函數fetchAll()出現非對象錯誤 –

+0

仍然不工作相同的錯誤「致命錯誤:調用成員函數fetchAll()時出現一個非對象錯誤」:(@ Nana Partykar –

+0

粘貼你的數據庫連接fil e在你的問題@ user1 –

0

您需要添加顯示錶中數據的sql查詢。嘗試$sellers = new ManageUsers();後,在列表seller.php加入這一行:

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

沒有使用fetchall我已經在$結果class.ManageUsers.php添加查詢...所以如何我可以得到這個($結果)到一個list-seller.php –

+0

nlo,在seller.php中加上這個 – learner

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(); 
0

嘗試使用$sellers->link->fetchAll(PDO::FETCH_ASSOC)

+0

致命錯誤:調用未定義的方法PDO :: fetchAll().... –