在 PHP 中循环遍历 mysql_fetch_array

问题描述:

所以我有以下几点:

$i = 0;
$records = mysql_num_rows($sections_query);
$row_sections = mysql_fetch_array($sections_query);
    foreach ($row_sections as $value) {
    echo $value . "<br />";
}

执行后$records的值变为4,DB每行有2列.第一列值为section_id",第二列值为section_name".

The value of $records becomes 4 after execution and the DB has 2 columns per row. The first column value is 'section_id' and the second is 'section_name'.

我想写一个循环来打印'section_name'的值,比如-

I want to write a loop that will print out the value of 'section_name' like-

echo "<h3>" . $row_sections[section_name]; . "</h3>";

在循环中,所以基本上我将有 3 个 h3,每个 h3 包含 'section_name' 中的 4 个不同值

within the loop, so basically I'll have 3 h3's each containing the 4 different values in 'section_name'

也许 foreach() 不是最好的循环?反正我很迷茫.

And maybe foreach() isn't the best loop to use? Anyway, I'm confused.

希望这是有道理的,并提前感谢您的帮助!

Hope that makes sense, and thanks in advance for the help!

$i = 0;
$records = mysql_num_rows($sections_query);
while($row_sections = mysql_fetch_array($sections_query))
{
    echo "<h3>" . $row_sections['section_name'] . "</h3>";
}

这将遍历每一行并打印部分名称.

That will go through each row and print the Section Name.