0

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>\n");
$num_rows = mysql_num_rows($result);
$row = mysql_fetch_array($result);

print("<h3>Actors</h3>\n");
print($num_rows);
if($num_rows == 0){
    print("<h3>No items are currently recorded in table Actors</h3>\n");
}
else{
    print("<select id=\"actors\" name=\"actors\">\n");
    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.

2 Answers 2

1

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>\n");
$num_rows = mysql_num_rows($result);

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

if($num_rows == 0)
{
    print("<h3>No items are currently recorded in table Actors</h3>\n");
}
else
{
    print("<select id=\"actors\" name=\"actors\">\n");
    while ($row = mysql_fetch_array($result))
    {
            print("<option>$row[0]</option>");
    )
    print("</select>");
    mysql_free_result($result);
}
Sign up to request clarification or add additional context in comments.

4 Comments

I starter writing something like that. You beat me to it + I suggest you change that to mysqli better too.
someone gave me -1, could you explain in detail?
i am not the one who downvoted you but if someone ask a question try to tell him where he/she is doing wrong rather than giving him a new code.
@RishabhRaj honestly, my English is bad. so there is no time to explain in detail. in later, I'll try to give more information. Many thanks.
1
 $query = "SELECT actor_name FROM actors";
    $result = mysql_query($query) or die("<h1>Error -
    the query could not be    executed</h1>\n");
    $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>");

    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.