将 mysql_fetch_array 结果转储到多维数组中 [英] Dump mysql_fetch_array results into a multidimensional array
问题描述
感谢所有的大师们.
我正在尝试在个人资料页面上制作上一个和下一个按钮.如果我正在查看 Arnsdorf 的个人资料,我想要一个链接到 Antonio 的上一个按钮和一个链接到 Baldeviso 的下一个按钮.
I am trying to make a previous and next button on a profile page. If I am looking at the profile for Arnsdorf, I want a previous button to link to Antonio and a next button to link to Baldeviso.
从按 lname 排序的输出中可以看出,ID 不是连续的:
IDs aren't sequential as you can see from the output which is ordered by lname:
ID: 22 Name: Airaghi
ID: 36 Name: Antonio
ID: 27 Name: Arnsdorf
ID: 13 Name: Baldeviso
ID: 46 Name: Barnes
这是我的代码:
$sql = "SELECT id,lname FROM profiles ORDER BY lname";
$query = mysql_query($sql);
if (!$query) {
die('Invalid query: ' . mysql_error());
}
while ($row = mysql_fetch_array($query, MYSQL_BOTH)) {
printf ("ID: %s Name: %s", $row[0], $row["lname"]);
echo "<br>";
}
mysql_free_result($query);
但我无法将其放入 array
中进行排序.
But I can't get it into an array
to sort it.
我希望创建一个 3 列的多维数组
,其中的数据集大致如下:
I was hoping to create a 3 column multi-dimensional array
with a data set that looks roughly like this:
row,id,lname
1,22,Airaghi
2,36,Antonio
3,27,Arnsdorf
4,13,Baldeviso
5,46,Barnes
然后我可以弄清楚如何获取 Arnsdorf 的行号.然后获取 +1 和 -1 的行号,以便我可以获取它们各自的 ID 以在我的超链接中使用.
I could then figure out how to get the number of the row for say Arnsdorf. Then get the row number for +1 and -1, so that I could get their respective ids for use in my hyperlink.
我已经找了几个小时了,但找不到任何代码示例可以将 mysql 数据转储到一个编号的多维数组中,该数组足够永久地进行此类排序.感谢您的帮助.
I have looked for hours and I can't find any code examples that will dump the mysql data into a numbered multi-dimensional array permanent enough to do this sort on. Thanks for your help.
推荐答案
您可以将每一行分配给数组,然后从该数组中回显数据.示例如下:
You can assing each row to array, and then echo data from that array. Here is example:
$persons = array();
while($row = mysql_fetch_assoc($query)) {
$persons[] = $row;
}
// To access ID of user in third row
echo $persons[2]['id'];
// Name of that person
echo $persons[2]['lname'];
这篇关于将 mysql_fetch_array 结果转储到多维数组中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!