user1 user1 - 1 month ago 8
MySQL Question

How to show table values in php using PDO

I'm trying to show table values from database by using PDO but while doing this it shows an error


"Fatal error: Call to undefined method ManageUsers::fetchAll()"


Please provide solution to solve this?

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';

}
}

}


?>

Answer

Changes

1) Remove include "C:/wamp/www/Super_Admin_MangoAir/classes/class.database.php"; from class ManageUsers (inside) and place it above.

2) Add public before seller_list().

3) call seller_list() in list-seller.php page.

4) Missed return keyword in function seller_list(). Add it.

5) Remove $result from seller_list() function in Class ManageUsers as it is of no use.

Updated Code

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();
  }
}
?>
Comments