使用 fetch_assoc 迭代 MYSQL 结果为 PHP

Using fetch_assoc to iterate through MYSQL results with PHP

我正在尝试使用 "fetch_assoc" 功能来收集 table.

中所有行的所有 "blog" 字段

目前,它有点工作;它在 first 行中收集 first "blog" 字段的内容,但是对于每隔一行,它从第一行。

例如,这是我使用的代码:

$connection = new mysqli($host,$user,$pass,$db);

$result = $connection->query("SELECT * FROM `Blogs`");
$blogs = array();
$max = sizeof($blogs);
while( $row = $result->fetch_assoc() ) {
    $blogs[] = $row['Blog_Contents'];
    for ($x = 0; $x <= $max; $x++) {
      echo ("<div align=center> <div class=container> <div class='well well-lg wow bounceIn' data-wow-delay='.1s'>" . $blogs[$x] . "</div> </div> </div>");
    } 
}

截至目前,table 中有四行 - 因此第 将具有:

Hello this is blog number 1 of the test scheme

两个将有:

Hello this is blog number 2 of the test scheme

3 和 4 也一样,博客中的数量随着它的索引而增加。

目前,我的代码产生以下结果:

Hello this is blog number 1 of the test scheme
Hello this is blog number 1 of the test scheme
Hello this is blog number 1 of the test scheme
Hello this is blog number 1 of the test scheme

谁能告诉我为什么我的代码没有读取另一个 Blog_Contents?

也许告诉我如何纠正代码?

对不起,如果我解释得不是很好;我已尽我所能对此进行研究,但找不到我需要的东西。 提前致谢,

Sparkhead95

在 while 循环中初始化 $max 的值

$result = $connection->query("SELECT * FROM `Blogs`");
$blogs = array();

while( $row = $result->fetch_assoc() ) {
    $blogs[] = $row['Blog_Contents'];
    $max = sizeof($blogs);
    for ($x = 0; $x <= $max; $x++) {
      echo ("<div align=center> <div class=container> <div class='well well-lg wow bounceIn' data-wow-delay='.1s'>" . $blogs[$x] . "</div> </div> </div>");
    } 
}

感谢@VolkerK 指出 - 我的代码中有这个:

$blogs = array(); $max = sizeof($blogs);

所以我的 $max 变量总是 0。

我将代码更改为:

$connection = new mysqli($host,$user,$pass,$db);

$result = $connection->query("SELECT * FROM `Blogs`");
$blogs = array();

while( $row = $result->fetch_assoc() ) {
    $blogs[] = $row['Blog_Contents'];
}
$max = sizeof($blogs);
for ($x = 0; $x <= $max; $x++) {
    echo ("<div align=center> <div class=container> <div class='well well-lg wow bounceIn' data-wow-delay='." . $x . "s'>" . $blogs[$x] . "</div> </div> </div>");
} 

现在可以使用了。

再次感谢。