Get fetchall result from different class

82 views Asked by At

let me explain what i'm trying to do.

In one DBcommand.php file i have :

class DBcommand Extends PdoDB { 
     public function selectAll() {
       $stmt = $this->dbh->prepare("SELECT hwid,username,pcname FROM users");
       $stmt->execute();
       $columnn = array();
       $result_returned = $stmt->fetchAll(PDO::FETCH_CLASS);
       foreach($result_returned as $key=>$result) {
         $columnn['hwid'] = $result->hwid;
         $columnn['username'] = $result->username;
         $columnn['pcname'] = $result->pcname;
         //return $columnn;
         //$columnn= new us($result->hwid,$result->username,$result->pcname);
       }
       return $columnn;
     }
}

And i'm trying to get my result on another page called View.php

$cmd = new DBcommand();
//$get = $cmd->getInfo('1234');
$result=$cmd->selectAll();

echo '<tr ><td ><input type="checkbox" class="checkthis" /></td><td>' . $result['hwid'] . '</td><td style="cursor:pointer;" onclick="alert(\'ok\')">' . $result['username'] . '</td><td style="cursor:pointer;" onclick="alert(\'ok\')">' . $result['pcname']. '</td><td><p data-placement="top" data-toggle="tooltip" title="Edit"><button class="btn btn-primary btn-xs" data-title="Edit" data-toggle="modal" data-target="#edit" ><span class="glyphicon glyphicon-download-alt" onclick="myAjax()"></span></button></p></td>
  <td><p data-placement="top" data-toggle="tooltip" title="Delete"><button class="btn btn-danger btn-xs" data-title="Delete" data-toggle="modal" data-target="#delete" ><span class="glyphicon glyphicon-trash"></span></button></p></td>
</tr>';
                                        ?>

The issue i have is , my selectall works , but my view.php only get 1 info from it, either the first one or last one.

Even if i try to make some kind of while on the view.php part, it never get all result.

lets say my selectAll return array('123','azerty',azerty'); and array('6547','qwertty',qwerty'); , my print_r from selectAll will show me what i want to see, but my result from view.php, will only show me one of these 2 result.

--

I even tried to create another class that takes $this->_hwid, so i'll use this class later, but didnt manage, since its object in string result.

--

Getting stuck, Thanks for your help.

2

There are 2 answers

1
Dexa On BEST ANSWER

Your foreach always fills just one item in the array.

try this instead:

 foreach($result_returned as $key=>$result) {
   $columnn[$key]['hwid'] = $result->hwid;
   $columnn[$key]['username'] = $result->username;
   $columnn[$key]['pcname'] = $result->pcname;
 }
0
Your Common Sense On
public function selectAll() {
    $stmt = $this->dbh->prepare("SELECT hwid,username,pcname FROM users");
    $stmt->execute();
    return $stmt->fetchAll();
}

this is ALL the code for the problem function you need.

your other code also requires minimal understanding of a technology you are using. At least you have to realize that you're getting multiple rows from the table, in the form of array, which usually being looped over using foreach() command.