我有一个主键名为“id”的表,我正在使用 ADODB 并取回 ADORecordSet_mysql。我需要一个数组,其中 id 与结果集中的一行相关联,但 ADODBRecordSet_mysql 似乎只有一个 GetArray(int startingRow) 方法返回由 startingRow 索引的数组(默认值为 0)。
我不想自己遍历这个结果集并将 id 关联到每一行,我不喜欢必须将起始索引传递给 GetArray 的想法。我宁愿能够使用我的主键将数组取回索引。
这可能吗?还是我的头在云端?
getAssoc() 方法 将完全满足您的需求:
// $db is connected ADOConnection object with
print_r($db->getAssoc('select * from employee'));
/*
Sample output with $ADODB_FETCH_MODE = ADODB_FETCH_ASSOC
assuming employee table with columns id, name, salary
Array
(
[100] => Array
(
[name] => Bob
[salary] => 10000
)
[103] => Array
(
[name] => Sally
[salary] => 12000
)
*/
如果您还需要在输出中包含 id,只需查询
SELECT id, e.* FROM employee e