类:ADORecordSet_mysql 获取按行 id 索引的数组的简单方法

问题描述 投票:0回答:1

我有一个主键名为“id”的表,我正在使用 ADODB 并取回 ADORecordSet_mysql。我需要一个数组,其中 id 与结果集中的一行相关联,但 ADODBRecordSet_mysql 似乎只有一个 GetArray(int startingRow) 方法返回由 startingRow 索引的数组(默认值为 0)。

我不想自己遍历这个结果集并将 id 关联到每一行,我不喜欢必须将起始索引传递给 GetArray 的想法。我宁愿能够使用我的主键将数组取回索引。

这可能吗?还是我的头在云端?

php mysql adodb
1个回答
0
投票

getAssoc() 方法 将完全满足您的需求:

  • 当请求 2 列时,第一列成为键,第二列成为值
  • 当请求超过 2 列时,第一列成为键,所有其他列成为该键下的数字数组。
// $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

© www.soinside.com 2019 - 2024. All rights reserved.