![](/img/trans.png)
[英]PHP, HTML and MySqli - how to show all data from specific column in one table row
[英]How do i call specific row and column data from an array after querying a database… (mysqli php)
问题:
我想使用一个数据库查询将信息存储到数组中...
然后,我希望能够从结果中称ROW为 “ 3”, COLUMN为 “ ARTIST”,并将其多次放置在我网站的不同区域,而无需重新查询数据库。
我做了什么
我能够查询数据库并在屏幕上显示该信息,但我知道这是朝我实际需要的错误方向迈出的一步。
<?php
require_once( “DbConnect.php”);
$ sql =“从历史列表ORDER BY date_played
DESC LIMIT 3中选择artist
, title
, label
, albumyear
, picture
。
$ result = $ db-> query($ sql);
while ($row=$result->fetch_object()) {
$artist = $row->artist;
$title = $row->title;
$label = $row->label;
$albumyear = $row->albumyear;
echo "$artist<br> $title<br> $label<br> $albumyear<br>";
}
?>
我相信我需要使用fetch_assoc,以便当我获取结果时将其绑定到列名,但是我可能是错的。
任何帮助将非常感激。
谢谢
贾斯汀。
只需放入一个数组中以便以后访问:
$albums = array();
while ($row=$result->fetch_object()) {
$albums[]['artist'] = $row->artist;
$albums[]['title'] = $row->title;
$albums[]['label'] = $row->label;
$albums[]['albumyear'] = $row->albumyear;
}
echo $albums[2]['artist']; // gets the third row artist.
由于PHP对数组使用基于零的索引,因此我们使用键“ 2”获得第三行。 如果要使用键“ 3”来引用第三行,则在创建数组时只需使用自己的计数器即可。
$albums = array();
$i = 1;
while ($row=$result->fetch_object()) {
$albums[$i]['artist'] = $row->artist;
$albums[$i]['title'] = $row->title;
$albums[$i]['label'] = $row->label;
$albums[$i]['albumyear'] = $row->albumyear;
$i++;
}
echo $albums[3]['artist']; // gets the third row artist.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.