PDO - 在一段时间内获取Assoc

时间:2016-08-23 19:13:49

标签: php mysql pdo

我试图在while循环中回显一些html。 我用PDO和PDO_ASSOC获取了我的东西。

这就是我所拥有的:

        $stmt = $this->conn->prepare('SELECT * FROM books');
        $stmt->execute();
        while($row = $stmt->fetch(PDO::FETCH_ASSOC))
        {
            $book_id = $row['id'];
            $book_title = $row['title'];
            $book_image = $row['image'];
            $book_amz = $row['amazon'];
            $book_desc = $row['description'];
            $book_rating = $row['rating'];
            $book_date = $row['date'];
            $book_author = $row['author'];
            $book_categorie = $row['categorie'];
            $text = "ID: ' . $book_id . '";
         }
         return $text;

但它只给我一行表。甚至尝试了fetchAll,但它没有给我任何东西。

3 个答案:

答案 0 :(得分:2)

因此假设唯一看到的元素是它的最后一个元素,因为你的返回被覆盖每个循环。有几个选项可以解决这个问题。最简单的是:

$stmt = $this->conn->prepare('SELECT * FROM books');
$stmt->execute();

$text = "";
while($row = $stmt->fetch(PDO::FETCH_ASSOC))
{
    $book_id = $row['id'];
    $book_title = $row['title'];
    $book_image = $row['image'];
    $book_amz = $row['amazon'];
    $book_desc = $row['description'];
    $book_rating = $row['rating'];
    $book_date = $row['date'];
    $book_author = $row['author'];
    $book_categorie = $row['categorie'];

    //String concatenation of text will 
    //give you one big string at the end to return.   
    $text .= "ID: '{$book_id}'";
}
return $text;

然而,这对你真正的bootstrap html不适用。您需要确保列合适。

你需要更直观的东西

使用实际代码看起来像

$stmt = $this->conn->prepare('SELECT * FROM books');
$stmt->execute();

$bookEcho = array();
while($row = $stmt->fetch(PDO::FETCH_ASSOC)){
    $bookEcho[] = '<div class="col-md-3">
                   <div class="thumbnail">
                   <span>' . $book_title . '</span>
                   <img src="' . $book_image . '">
                   <div class="book-options">
                   <span>Bewertung</span><br/>
                   ' . $stars . '
                   <a href="books.php?id=' . $book_id . '" class="btn btn-read btn-block">Jetzt lesen</a>
                   </div>
                   </div>
                   </div>';
}
return $bookEcho;

现在在你的功能中,你可以做的事情(这不是我写过的最优雅的东西,但应该完成工作):

$cols = 4;
$colCount = 1;
foreach ($bookEcho as $book){
    if($colCount == 0){//create a row}
    echo $book;
    $coolCount++;
    if($colCount == 0){end a row}
    if($colCount == 4){ $colCount = 0;}
}

答案 1 :(得分:1)

问题是您在while循环中覆盖了您的值。对于数据库中的每个条目,循环正在执行一次,但只返回最后一个循环。相反,你想使用数组:

$stmt = $this->conn->prepare('SELECT * FROM books');
$stmt->execute();
$books = [];
while($row = $stmt->fetch(PDO::FETCH_ASSOC))
{
    $book['id'] = $row['id'];
    $book['title'] = $row['title'];
    $book['image'] = $row['image'];
    $book['amz'] = $row['amazon'];
    $book['desc'] = $row['description'];
    $book['rating'] = $row['rating'];
    $book['date'] = $row['date'];
    $book['author'] = $row['author'];
    $book['categorie'] = $row['categorie'];
    $book['text'] = "ID: ' . {$book['id']} . '"; // << Not sure if this is what you actually want. If not, adjust accordingly.

    // Append the above values to the $books array
    $books[] = $book;
}
return $books;

答案 2 :(得分:-1)

为什么不使用foreach?

$books = $stmt->fetchAll(PDO::FETCH_ASSOC);
foreach($books as $book){
    $book_id = $book['id'];
    $book_title = $book['title'];
    // ...
}