这是我的查询:
$query = "SELECT last_name, first_name, DOB, site, COUNT(*) AS Num FROM student "
. "WHERE site = '{$siteRow['site']}' "
. "GROUP BY last_name, first_name, DOB having Num > 1";
当我在 MySQL 客户端上运行它时,我得到了相应的行。 last_name
、first_name
、DOB
、site
和 Num
但是,当我尝试通过 php
执行此操作(并使用 var_dump
进行测试)时,我得到了相同的五个 col;umns,但在每个之前,我获取 0
、1
、2
、3
和 4
- 每个对应上面的 5 列按顺序书写。
这是 php
:
$result = $DB->query($query);
$cnt = $DB->count($result);
if ($cnt > 0) {
while ($row = $DB->fetch_array($result)) { // solved; issue here!
if (!in_array(strtolower(trim($row['site'])), $dup_student_sites)) {
$records[] = $row;
$dup_student_sites[] = strtolower(trim($row['site']));
$dupCount++;
}
}
}
var_dump($records); // tested here to see result with row numbers
这是 var_dump 的输出(部分):
array(111) {
[0] =>
array(10) {
[0] =>
string(7) "---"
's_s_last_name' =>
string(7) "---"
[1] =>
string(4) "---"
's_s_first_name' =>
string(4) "---"
[2] =>
string(10) "2003-03-20"
's_s_DOB' =>
string(10) "2003-03-20"
[3] =>
string(8) "---"
's_s_site' =>
string(8) "---"
[4] =>
string(1) "2"
'Num' =>
string(1) "2"
}
最佳答案
我通常不会给出如此淡化的答案,但是:
可能的问题是 $DB->fetch_array
可能的解决方案是将其更改为 $DB->fetch_assoc
解释
与这个答案相同的想法:
关于php - 在 PHP/MySQL 中获取具有实际列名的列号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20199388/