从PHP MySQL数组填充HTML选择列表

I'm fetching a list of 'actors' from my MySQL database table actors & trying to populate the result into a HTML Select box:

    <?php
$query = "SELECT actor_name FROM actors";
$result = mysql_query($query) or die("<h1>Error - the query could not be executed</h1>
");
$num_rows = mysql_num_rows($result);
$row = mysql_fetch_array($result);

print("<h3>Actors</h3>
");
print($num_rows);
if($num_rows == 0){
    print("<h3>No items are currently recorded in table Actors</h3>
");
}
else{
    print("<select id=\"actors\" name=\"actors\">
");
    for($i = 0; $i < $num_rows; $i++){
        print("<option>$row[$i]</option>");
        $row = mysql_fetch_array($result);
    }
    print("</select>");
}
?>

Error: Notice: Undefined offset: 1 in C:\xampp\htdocs\actors.php on line 16

I'm getting a Undefined offset notice from the second record in the array onwards. When I add an isset check the select box is only populated with the first record. This would indicate that there is something wrong my query? I've checked my table, and there are 113 records.

Any help would be greatly appreciated.

Cheers,

Sam

2个回答

I think below code looks better and works well.

$query = "SELECT actor_name FROM actors";
$result = mysql_query($query) or die("<h1>Error - the query could not be executed</h1>
");
$num_rows = mysql_num_rows($result);

print("<h3>Actors</h3>
");
print($num_rows);

if($num_rows == 0)
{
    print("<h3>No items are currently recorded in table Actors</h3>
");
}
else
{
    print("<select id=\"actors\" name=\"actors\">
");
    while ($row = mysql_fetch_array($result))
    {
            print("<option>$row[0]</option>");
    )
    print("</select>");
    mysql_free_result($result);
}
dongyao1915
dongyao1915 谢谢你的帮助。
接近 7 年之前 回复
dongliping003116
dongliping003116 老实说,我的英语不好。 所以没有时间详细解释。 在稍后,我会尝试提供更多信息。 非常感谢。
接近 7 年之前 回复
douao1854
douao1854 别客气 :)
接近 7 年之前 回复
dongmai6666
dongmai6666 谢谢你的建议;-)
接近 7 年之前 回复
douchunji1885
douchunji1885 我不是那个贬低你的人,但如果有人问一个问题,试着告诉他他/她做错了什么而不是给他一个新的代码。
接近 7 年之前 回复
dsj83686
dsj83686 有人给了我-1,你能详细解释一下吗?
接近 7 年之前 回复
dongyan1548
dongyan1548 谢谢 ;-)
接近 7 年之前 回复
douan2907
douan2907 当然,使用mysqli,或PDO而不是mysql_xxx()
接近 7 年之前 回复
duangejian6657
duangejian6657 我开始写这样的东西。 你打败了我+我建议你更好地改变它到mysqli。
接近 7 年之前 回复
 $query = "SELECT actor_name FROM actors";
    $result = mysql_query($query) or die("<h1>Error -
    the query could not be    executed</h1>
");
    $num_rows = mysql_num_rows($result);
    $row = mysql_fetch_array($result);
-`--------^^^^^^ //**you are already fetching the array once`**

you are using $row = mysql_fetch_array($result); twice

for($i = 0; $i < $num_rows; $i++){
        print("<option>$row[$i]</option>");
        $row = mysql_fetch_array($result);
----------------^^^^//**remove this**

    }

the correct loop will be:

for($i = 0; $i < $num_rows; $i++){
        print("<option>$row[$i]</option>");

    }
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐