Jörg Reinholz: Ein Array mit assotiativem Feld versehen

Beitrag lesen

Moin!

$L = $QuE->fetchAll(PDO::FETCH_GROUP);

Ich würde lieber sowas haben:
$L['home']['Link']

Jemand eine Idee?

Ja. $sth->fetchAll(PDO::FETCH_CLASS, "objektname")

Beispiel #4 Instantiating a class for each result

<?php  
# objekt bauen  
class fruit {  
    public $name;  
    public $colour;  
}  
  
$sth = $dbh->prepare("SELECT name, colour FROM fruit");  
$sth->execute();  
  
$result = $sth->fetchAll(PDO::FETCH_CLASS, "fruit");  
var_dump($result);  
?>

Das oben gezeigte Beispiel erzeugt eine ähnliche Ausgabe wie:

array(3) {  
  [0]=>  
  object(fruit)#1 (2) {  
    ["name"]=> string(5) "apple"  
    ["colour"]=> string(5) "green"  
  }  
  [1]=>  
  object(fruit)#2 (2) {  
    ["name"]=> string(4) "pear"  
    ["colour"]=> string(6) "yellow"  
  }  
  [2]=>  
  object(fruit)#3 (2) {  
    ["name"]=> string(10) "watermelon"  
    ["colour"]=> string(4) "pink"  
  }  
}  

ODER Du nimmst mysqli:

<?php
$L=array();
$mysqli = new mysqli("localhost", "user", "password", "database");
$mysqli->real_query("SELECT id, foo, bar FROM test");
$res = $mysqli->use_result();
while ($row = $res->fetch_assoc()) { $L[]=$row; }
print_($L);
?>