开发者

php reading and associative array from multiple database records

I know there have been many questions on associative arrays, but I haven't seen one that explains what I have to do to extract the data from this way of creating one. Assume that I have selected multiple records from the same field in the database.

    while ($Row = mysql_fetch_array($mySQLData))
    {
    $assocarraydata[]= array('field1'=>$Row["field1"]);
    }; 

I know I can do it this way using the index:

    //Put Data into an regular array
    while ($Row = mysql_fetch_array($mySQLData))
    {
    $field1[] = $Row[field1];
    }

    echo $field1[1];

But I 开发者_运维技巧don't know how to do it for the associative array in the first example. Can someone help?


Hard to figure out what you're asking, but if the question is how to produce the same result from the first example, the answer is:

// echo the value of field1 from the second (index 1) row of data
echo $assocarraydata[1]['field1'];


You need to have an index in the resulting array - otherwise you won't be able to access different rows. Just think of the indexes as row numbers:

while ($row = mysql_fetch_assoc($result))
    $array[] = $row;

... and then you can access your data like this:

$array[0]['field1'] - first row, column 'field1' $array[5]['field5'] - sixth row, column 'field5'

... etc.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜