0

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.

wolffer-east
  • 1,069
  • 7
  • 14
snow
  • 162
  • 1
  • 13
  • You're overwriting values in your `foreach` loop. And why are you injecting an object of `PdoDB` in `DBcommand` you're alreading extending it. – Daan Jun 25 '15 at 14:32
  • Yes thanks i corrected the DB thing, it was due to an old test. Yes in this exemple, i am overwritting, but even if i put the return inside the foreach, result remind the same – snow Jun 25 '15 at 14:33

2 Answers2

0
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.

Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
-1

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;
 }
Dexa
  • 1,641
  • 10
  • 25