[英]Get MySQL column names in PHP array
Can someone please help me how to get my MySQL column names out in my while loop? 有人可以帮我如何在我的while循环中获取我的MySQL列名称吗? My code goes like this:
我的代码是这样的:
if (mysqli_connect_errno()) {
echo "MySQL failed: " . mysqli_connect_errno();
}
if (isset($_GET['date'])) {
$get_date = $_GET['date'];
$result = mysqli_query($conn, "SELECT * FROM stats WHERE form_date = '$get_date'");
$col = array();
while ($row = mysqli_fetch_array($result)) {
$col[] = $row['year'];
$col[] = $row['month'];
$col[] = $row['day'];
$col[] = $row['weekday'];
$col[] = $row['form_date'];
$col[] = $row['name'];
$col[] = $row['count'];
$col[] = $row['lang'];
}
echo "<pre>";
print_r($col);
echo "</pre>";
mysqli_close($conn);
} else {
echo "Please set a date value in URL parameter ?date=[YYYY-MM-DD]";
}
My returned array then looks like this: 我返回的数组如下所示:
Array
(
[0] => 2014
[1] => 01
[2] => 01
[3] => Wednesday
[4] => 2014-01-01
[5] => Michael K
[6] => 41
[7] => SE
[8] => 2014
[9] => 01
[10] => 01
[11] => Wednesday
[12] => 2014-01-01
[13] => Nicklas S
[14] => 40
[15] => DK
[16] => 2014
[17] => 01
[18] => 01
[19] => Wednesday
[20] => 2014-01-01
[21] => Jesper S
[22] => 5
[23] => SE
)
However I would like my array to get returned like this: 但是我希望我的数组像这样返回:
Array
(
[year] => 2014
[day] => 01
[month] => 01
[weekday] => Wednesday
[form_date] => 2014-01-01
[name] => Michael K
[count] => 41
[lang] => SE
[year] => 2014
[month] => 01
[day] => 01
[weekday] => Wednesday
[form_date] => 2014-01-01
[name] => Nicklas S
[count] => 40
[lang] => DK
[year] => 2014
[month] => 01
[day] => 01
[weekday] => Wednesday
[form_date] => 2014-01-01
[name] => Jesper S
[count] => 5
[lang] => SE
)
So the keys in my array matches the column names I have in my database. 因此,数组中的键与数据库中的列名称匹配。 I just can't seem to wrap my head around how I do this.
我似乎无法全神贯注于我如何做到这一点。 Can someone help?
有人可以帮忙吗? :-)
:-)
Change 更改
$col = array();
while ($row = mysqli_fetch_array($result)) {
$col[] = $row['year'];
$col[] = $row['month'];
$col[] = $row['day'];
$col[] = $row['weekday'];
$col[] = $row['form_date'];
$col[] = $row['name'];
$col[] = $row['count'];
$col[] = $row['lang'];
}
to 至
$col = array();
while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
$col[] = $row;
}
print_r($col);
mysql_fetch_array() has second argument. mysql_fetch_array()具有第二个参数。 This optional parameter is a constant indicating what type of array should be produced from the current row data.
此可选参数是一个常量,指示应从当前行数据中生成哪种类型的数组。 The possible values for this parameter are the constants MYSQLI_ASSOC, MYSQLI_NUM, or MYSQLI_BOTH.
此参数的可能值为常数MYSQLI_ASSOC,MYSQLI_NUM或MYSQLI_BOTH。
You are looking for mysqli_fetch_assoc()
which fetches results as an associative array (column => value). 您正在寻找
mysqli_fetch_assoc()
,它以关联数组(列=>值)的形式获取结果。
Then try this: 然后试试这个:
while ($row = mysqli_fetch_assoc($result)) {
echo "<pre>";
print_r($row);
echo "</pre>";
}
You can not use an associative array like this because it will have duplicate key indices, which is not allowed. 您不能使用这样的关联数组,因为它将具有重复的键索引,这是不允许的。 I suggest you work with a restructured array to have each "group" of keys as a nested array.
我建议您使用重组数组,以使每个“组”键都为嵌套数组。
使用mysql_assoc_array()
代替mysq_fetch_array()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.