简体   繁体   English

如何访问 mysql_fetch_array 的返回值的元素?

[英]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?当我写这篇文章时,它给了我 194 个元素的数组,但是当我回显它们时,它只写 ArrayArrayArray....... 194 次知道为什么它没有给出国家的名称吗?

You have to specify which column you want out of your $answer -array.您必须从$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. mysql_fetch_array 返回一个数组,其键值是查询表中的字段名称,其值是该表中该行的值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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