简体   繁体   中英

How to access elements of the return value of mysql_fetch_array?

$con = mysql_connect("servername","username","password");
        if (!$con){die('Could not connect: ' . mysql_error());}
            mysql_select_db("Appiness", $con);

    $result= mysql_query("SELECT * FROM country");
    while($answer= mysql_fetch_array($result))
    {
        echo $answer;
    }

When i write this it gives me my array of 194 elements but when i echo them it only writes ArrayArrayArray....... 194 times any idea why it is not giving the names of the countries?

You have to specify which column you want out of your $answer -array. If the column name is name:

echo $answer["name"]
while($answer= mysql_fetch_array($result))
{
    echo implode("\t", $answer) . "\n";
}

to get all fields, or

while($answer= mysql_fetch_array($result))
{
    echo "$answer[0]\n";
}

to get the first field, etc.

You need to specify the filed that you want to display. mysql_fetch_array returns an array whose key values are the field names from the queried table and whose values are the values in that table for that row.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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