这真的很奇怪......如果pdo fetch模式是assoc,它会返回错误的id列值。如果mode是两者,则返回错误的'id'值,但[0]没问题。 表结构
mysql> describe itemweap;
+---------------------+-----------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------------------+-----------------+------+-----+---------+-------+
| id | int(11) | NO | | NULL | |
| userID | int(11) | NO | | NULL | |
| Tier | int(1) | NO | | NULL | |
| damage | int(7) unsigned | NO | | NULL | |
| MaxUpgrades | int(2) | NO | | NULL | |
| CurrentUpgradeLevel | int(2) | NO | | NULL | |
| heroID | int(1) unsigned | NO | | NULL | |
| status | int(1) unsigned | NO | | NULL | |
| hash | char(32) | NO | PRI | NULL | |
+---------------------+-----------------+------+-----+---------+-------+
代码
function performSingleQuery($DBH, $query,$data) {
$STH = $DBH->prepare($query);
if (!isset($STH))
echo "STH NOT SET<br />";
else if ($STH) {
try {
if (isset($data))
$STH->execute($data);
else $STH->execute();
if ($STH->errorCode()[2] == 0) {
return $STH->fetchAll();
}
else return null;
}
catch (PODexception $e) {
echo $e->errorInfo;
return null;
}
}
return null;
}
function performQueryAndGenerateTable($isSearchSpecific, $query, $exNTAR, $DBH,
$isProfile, $tableName, $title, $mode) {
$rows = performSingleQuery($DBH, $query, null);
print_r($rows); //checking values here
...
}
查询示例:
SELECT A.id, A.userID, A.tier, A.damage, A.maxupgrades, A.heroID, A.CurrentUpgradeLevel, c.herohealth, d.heropower , e.herospellcastrate, f.heromovementspeed, g.herooffabpow, h.herodefabpow, i.itemweapeledmg, j.itemaddproj, k.defencehealth, L.defencepower, M.defencerange, N.defencespeed, O.itemweaprangedaltdmg, R.id, S.notes
from itemweap A
left join herohealth C on (C.id = A.id)
left join heropower D on (D.id = A.id)
left join herospellcastrate E on (E.id = A.id)
left join heromovementspeed F on (F.id = A.id)
left join herooffabpow G on (G.id = A.id)
left join herodefabpow H on (H.id = A.id)
left join itemweapeledmg I on (I.id = A.id)
left join itemaddproj J on (J.id = A.id)
left join defencehealth K on (K.id = A.id)
left join defencepower L on (L.id = A.id)
left join defencerange M on (M.id = A.id)
left join defencespeed N on (N.id = A.id)
left join itemweaprangedaltdmg O on (O.id = A.id)
left join users R on (R.id = A.userID)
left join notes S on (S.id = A.id) WHERE A.userID = 4 AND A.status = 1;
此查询结果来自mysql控制台 http://i.imgur.com/hywrvC3.jpg(非常宽的图像) id列在这里很好。 print_r输出
Array ( [0] => Array ( [id] => 4 [userID] => 4 [tier] => 2 [damage] => 2 [maxupgrades] => 5 [heroID] => 3 [CurrentUpgradeLevel] => 5 [herohealth] => [heropower] => [herospellcastrate] => [heromovementspeed] => [herooffabpow] => [herodefabpow] => [itemweapeledmg] => 33 [itemaddproj] => [defencehealth] => [defencepower] => 55 [defencerange] => [defencespeed] => [itemweaprangedaltdmg] => [notes] => sdasdawdasdasda )
[1] => Array ( [id] => 4 [userID] => 4 [tier] => 2 [damage] => 155 [maxupgrades] => 5 [heroID] => 3 [CurrentUpgradeLevel] => 5 [herohealth] => [heropower] => [herospellcastrate] => [heromovementspeed] => [herooffabpow] => [herodefabpow] => [itemweapeledmg] => 55 [itemaddproj] => [defencehealth] => [defencepower] => 5 [defencerange] => [defencespeed] => [itemweaprangedaltdmg] => [notes] => )
[2] => Array ( [id] => 4 [userID] => 4 [tier] => 2 [damage] => 1200 [maxupgrades] => 3 [heroID] => 4 [CurrentUpgradeLevel] => 3 [herohealth] => 12 [heropower] => [herospellcastrate] => [heromovementspeed] => [herooffabpow] => [herodefabpow] => [itemweapeledmg] => 259 [itemaddproj] => 4 [defencehealth] => [defencepower] => [defencerange] => [defencespeed] => [itemweaprangedaltdmg] => 16 [notes] => ) )
每行的ID值都会被破坏。 但如果我将获取模式更改为两者,print_r会给我
Array ( [0] => Array ( [id] => 4 [0] => 90 [userID] => 4 [1] => 4 [tier] => 2 [2] => 2 [damage] => 2 [3] => 2 [maxupgrades] => 5 [4] => 5 [heroID] => 3 [5] => 3 [CurrentUpgradeLevel] => 5 [6] => 5 [herohealth] => [7] => [heropower] => [8] => [herospellcastrate] => [9] => [heromovementspeed] => [10] => [herooffabpow] => [11] => [herodefabpow] => [12] => [itemweapeledmg] => 33 [13] => 33 [itemaddproj] => [14] => [defencehealth] => [15] => [defencepower] => 55 [16] => 55 [defencerange] => [17] => [defencespeed] => [18] => [itemweaprangedaltdmg] => [19] => [20] => 4 [notes] => sdasdawdasdasda [21] => sdasdawdasdasda )
[1] => Array ( [id] => 4 [0] => 91 [userID] => 4 [1] => 4 [tier] => 2 [2] => 2 [damage] => 155 [3] => 155 [maxupgrades] => 5 [4] => 5 [heroID] => 3 [5] => 3 [CurrentUpgradeLevel] => 5 [6] => 5 [herohealth] => [7] => [heropower] => [8] => [herospellcastrate] => [9] => [heromovementspeed] => [10] => [herooffabpow] => [11] => [herodefabpow] => [12] => [itemweapeledmg] => 55 [13] => 55 [itemaddproj] => [14] => [defencehealth] => [15] => [defencepower] => 5 [16] => 5 [defencerange] => [17] => [defencespeed] => [18] => [itemweaprangedaltdmg] => [19] => [20] => 4 [notes] => )
[2] => Array ( [id] => 4 [0] => 83 [userID] => 4 [1] => 4 [tier] => 2 [2] => 2 [damage] => 1200 [3] => 1200 [maxupgrades] => 3 [4] => 3 [heroID] => 4 [5] => 4 [CurrentUpgradeLevel] => 3 [6] => 3 [herohealth] => 12 [7] => 12 [heropower] => [8] => [herospellcastrate] => [9] => [heromovementspeed] => [10] => [herooffabpow] => [11] => [herodefabpow] => [12] => [itemweapeledmg] => 259 [13] => 259 [itemaddproj] => 4 [14] => 4 [defencehealth] => [15] => [defencepower] => [16] => [defencerange] => [17] => [defencespeed] => [18] => [itemweaprangedaltdmg] => 16 [19] => 16 [20] => 4 [notes] => [21] => ) )
关联部分再次出错,但索引部分没问题。如何让fetch_assoc返回正确的值?
答案 0 :(得分:2)
id
键指的是查询中R.id的值。您需要在查询中为字段名称设置别名,以使它们都是唯一的。
e.g:
SELECT A.id AS A_id, .... ,R.id AS R_id ....
然后,您的关联数组将A_id
和R_id
作为具有适当值的键。