简体   繁体   English

PDO-暂时获取Assoc

[英]PDO - Fetch Assoc in a while

I'm trying to echo a bit of html within a while loop. 我试图在while循环中回显一些html。 I'm getting my stuff with PDO and PDO_ASSOC. 我正在使用PDO和PDO_ASSOC进行操作。

This is what I have: 这就是我所拥有的:

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

but it gives me only one row of the table. 但是它只给我桌子的一排。 Even tried fetchAll, but it gives me nothing. 甚至尝试过fetchAll,但它什么也没给我。

So making the assumption that the only element ever seen is the last element it is because what your are returning is being overwritten each loop. 因此,假设所见过的唯一元素是最后一个元素,是因为每个循环都将覆盖您返回的内容。 There are a few options to resolve this. 有一些解决方案。 The simplest is: 最简单的是:

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

However this will not work well with your real bootstrap html. 但是,这不能与您的实际引导HTML一起很好地工作。 You need to make sure that the columns add up right. 您需要确保各列的总和正确。

You will need something a bit more intuitive 您将需要一些更直观的东西

Using the actual code it would look something like 使用实际的代码看起来像

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

Now in your function what ever it is you can do something like (this is not the most elegant thing I have ever written but should get the job done): 现在在您的函数中可以执行任何操作(这不是我写过的最优雅的东西,但是应该完成工作):

$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;}
}

The problem is that you're overwriting your values in your while loop. 问题在于您正在覆盖while循环中的值。 The loop is being executed once for each entry in the database, but only the last one will be returned. 该循环对于数据库中的每个条目执行一次,但是仅返回最后一个。 Instead you want to use arrays: 相反,您想使用数组:

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

Why not use a foreach? 为什么不使用foreach?

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM