Danny Togaer
Danny Togaer

Reputation: 339

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?

Upvotes: 1

Views: 1094

Answers (3)

Tash Pemhiwa
Tash Pemhiwa

Reputation: 7685

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.

Upvotes: 0

Quassnoi
Quassnoi

Reputation: 425331

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.

Upvotes: 2

Jacob
Jacob

Reputation: 43219

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

echo $answer["name"]

Upvotes: 3

Related Questions