查询结果
数据库类包含许多用于处理查询结果集的方法,您可以在执行SELECT查询后获取这些结果集。
单值结果
loadResult()
当您期望从数据库查询中返回单个值时,请使用 loadResult()
。
id | name | username | |
---|---|---|---|
1 | John Smith | [email protected] | johnsmith |
2 | Magda Hellman | [email protected] | magdah |
3 | Yvonne de Gaulle | [email protected] | ydegaulle |
这通常是“计数”查询的结果,用于获取记录的数量
use Joomla\CMS\Factory;
$db = Factory::getContainer()->get('DatabaseDriver');
$query = $db->getQuery(true);
$query->select('COUNT(*)');
$query->from($db->quoteName('#__my_table'));
$query->where($db->quoteName('name')." = :value");
$query->bind('value', $value)
// Reset the query using our newly populated query object.
$db->setQuery($query);
$count = $db->loadResult();
或者您只在查找表中单行中的单个字段(或可能返回的第一行中的单个字段)。
use Joomla\CMS\Factory;
$db = Factory::getContainer()->get('DatabaseDriver');
$query = $db->getQuery(true);
$query->select('field_name');
$query->from($db->quoteName('#__my_table'));
$query->where($db->quoteName('some_name')." = :value");
$query->bind(':value', $some_value);
$db->setQuery($query);
$result = $db->loadResult();
单行结果
这些结果函数中的每一个都将从数据库返回单个记录,即使可能有多个记录满足您设置的条件。要获取更多记录,您需要再次调用该函数。
id | name | username | |
---|---|---|---|
1 | John Smith | [email protected] | johnsmith |
2 | Magda Hellman | [email protected] | magdah |
3 | Yvonne de Gaulle | [email protected] | ydegaulle |
loadRow()
loadRow()
从表中的单个记录返回一个索引数组
. . .
$db->setQuery($query);
$row = $db->loadRow();
print_r($row);
将给出
Array ( [0] => 1, [1] => John Smith, [2] => [email protected], [3] => johnsmith )
您可以使用以下方法访问各个值
$row['index'] // e.g. $row['2']
注意
- 数组索引从零开始。
- 虽然您可以重复调用以获取更多行,但返回多行的函数之一可能更有用。
loadAssoc()
loadAssoc()
从表中的单个记录返回一个关联数组
. . .
$db->setQuery($query);
$row = $db->loadAssoc();
print_r($row);
将给出
Array ( [id] => 1, [name] => John Smith, [email] => [email protected], [username] => johnsmith )
您可以使用以下方法访问各个值
$row['name'] // e.g. $row['email']
虽然您可以重复调用以获取更多行,但返回多行的函数之一可能更有用。
loadObject()
loadObject()
从表中的单个记录返回一个 PHP 对象
. . .
$db->setQuery($query);
$result = $db->loadObject();
print_r($result);
将给出
stdClass Object ( [id] => 1, [name] => John Smith, [email] => [email protected], [username] => johnsmith )
您可以使用以下方法访问各个值
$result->index // e.g. $result->email
虽然您可以重复调用以获取更多行,但返回多行的函数之一可能更有用。
单列结果
这些结果函数中的每一个都将从数据库返回单个列。
id | name | username | |
---|---|---|---|
1 | John Smith | [email protected] | johnsmith |
2 | Magda Hellman | [email protected] | magdah |
3 | Yvonne de Gaulle | [email protected] | ydegaulle |
loadColumn()
loadColumn()
从表中的单个列返回一个索引数组
$query->select('name'));
->from . . .";
. . .
$db->setQuery($query);
$column = $db->loadColumn();
print_r($column);
将给出
Array ( [0] => John Smith, [1] => Magda Hellman, [2] => Yvonne de Gaulle )
您可以使用以下方法访问各个值
$column['index'] // e.g. $column['2']
- 数组索引从零开始。
loadColumn()
等效于loadColumn(0)
。
loadColumn($index)
loadColumn($index)
从表中的单个列返回一个索引数组
$query->select(array('name', 'email', 'username'));
->from . . .";
. . .
$db->setQuery($query);
$column = $db->loadColumn(1);
print_r($column);
将给出
[
[0] => [email protected],
[1] => [email protected],
[2] => [email protected],
];
您可以使用以下方法访问各个值
$column['index'] // e.g. $column['2']
loadColumn($index)
允许您遍历结果中的系列列
. . .
$db->setQuery($query);
for ( $i = 0; $i <= 2; $i++ ) {
$column = $db->loadColumn($i);
print_r($column);
}
将给出
Array (
[0] => Array ( [0] => John Smith, [1] => Magda Hellman, [2] => Yvonne de Gaulle ),
[1] => Array ( [0] => [email protected], [1] => [email protected], [2] => [email protected] ),
[2] => Array ( [0] => johnsmith, [1] => magdah, [2] => ydegaulle )
)
数组索引从零开始。
多行结果
这些结果函数中的每一个都将从数据库返回多个记录。
id | name | username | |
---|---|---|---|
1 | John Smith | [email protected] | johnsmith |
2 | Magda Hellman | [email protected] | magdah |
3 | Yvonne de Gaulle | [email protected] | ydegaulle |
loadRowList()
loadRowList()
从查询返回的表记录中返回索引数组的索引数组
. . .
$db->setQuery($query);
$row = $db->loadRowList();
print_r($row);
将给出(为清楚起见添加了换行符)
Array (
[0] => Array ( [0] => 1, [1] => John Smith, [2] => [email protected], [3] => johnsmith ),
[1] => Array ( [0] => 2, [1] => Magda Hellman, [2] => [email protected], [3] => magdah ),
[2] => Array ( [0] => 3, [1] => Yvonne de Gaulle, [2] => [email protected], [3] => ydegaulle )
)
您可以使用以下方法访问各个行
$row['index'] // e.g. $row['2']
您可以使用以下方法访问各个值
$row['index']['index'] // e.g. $row['2']['3']
数组索引从零开始。
loadAssocList()
loadAssocList()
从查询返回的表记录中返回关联数组的索引数组
. . .
$db->setQuery($query);
$row = $db->loadAssocList();
print_r($row);
将给出(为清楚起见添加了换行符)
Array (
[0] => Array ( [id] => 1, [name] => John Smith, [email] => [email protected], [username] => johnsmith ),
[1] => Array ( [id] => 2, [name] => Magda Hellman, [email] => [email protected], [username] => magdah ),
[2] => Array ( [id] => 3, [name] => Yvonne de Gaulle, [email] => [email protected], [username] => ydegaulle )
)
您可以使用以下方法访问各个行
$row['index'] // e.g. $row['2']
您可以使用以下方法访问各个值
$row['index']['column_name'] // e.g. $row['2']['email']
loadAssocList($key)
loadAssocList($key)
返回一个关联数组 - 以“key”为索引 - 从查询返回的表记录中的关联数组
. . .
$db->setQuery($query);
$row = $db->loadAssocList('username');
print_r($row);
将给出(为清楚起见添加了换行符)
Array (
[johnsmith] => Array ( [id] => 1, [name] => John Smith, [email] => [email protected], [username] => johnsmith ),
[magdah] => Array ( [id] => 2, [name] => Magda Hellman, [email] => [email protected], [username] => magdah ),
[ydegaulle] => Array ( [id] => 3, [name] => Yvonne de Gaulle, [email] => [email protected], [username] => ydegaulle )
)
您可以使用以下方法访问各个行
$row['key_value'] // e.g. $row['johnsmith']
您可以使用以下方法访问各个值
$row['key_value']['column_name'] // e.g. $row['johnsmith']['email']
键必须是表中的有效列名;它不必是索引或主键。但如果它没有唯一值,则您可能无法可靠地检索结果。
loadAssocList($key, $column)
loadAssocList('key', 'column')
返回一个关联数组,以“key”为索引,查询返回的“column”命名列中的值
. . .
$db->setQuery($query);
$row = $db->loadAssocList('id', 'username');
print_r($row);
将给出(为清楚起见添加了换行符)
Array (
[1] => John Smith,
[2] => Magda Hellman,
[3] => Yvonne de Gaulle,
)
键必须是表中的有效列名;它不必是索引或主键。但如果它没有唯一值,则您可能无法可靠地检索结果。
loadObjectList()
loadObjectList()
从查询返回的表记录中返回 PHP 对象的索引数组
. . .
$db->setQuery($query);
$row = $db->loadObjectList();
print_r($row);
将给出(为清楚起见添加了换行符)
Array (
[0] => stdClass Object ( [id] => 1, [name] => John Smith,
[email] => [email protected], [username] => johnsmith ),
[1] => stdClass Object ( [id] => 2, [name] => Magda Hellman,
[email] => [email protected], [username] => magdah ),
[2] => stdClass Object ( [id] => 3, [name] => Yvonne de Gaulle,
[email] => [email protected], [username] => ydegaulle )
)
您可以使用以下方法访问各个行
$row['index'] // e.g. $row['2']
您可以使用以下方法访问各个值
$row['index']->name // e.g. $row['2']->email
loadObjectList($key)
loadObjectList('key')
返回一个关联数组 - 以“key”为索引 - 从查询返回的表记录中的对象
. . .
$db->setQuery($query);
$row = $db->loadObjectList('username');
print_r($row);
将给出(为清楚起见添加了换行符)
Array (
[johnsmith] => stdClass Object ( [id] => 1, [name] => John Smith,
[email] => [email protected], [username] => johnsmith ),
[magdah] => stdClass Object ( [id] => 2, [name] => Magda Hellman,
[email] => [email protected], [username] => magdah ),
[ydegaulle] => stdClass Object ( [id] => 3, [name] => Yvonne de Gaulle,
[email] => [email protected], [username] => ydegaulle )
)
您可以使用以下方法访问各个行
$row['key_value'] // e.g. $row['johnsmith']
您可以使用以下方法访问各个值
$row['key_value']->column_name // e.g. $row['johnsmith']->email
键必须是表中的有效列名;它不必是索引或主键。但如果它没有唯一值,则您可能无法可靠地检索结果。
其他结果集方法
getNumRows()
getNumRows()
将返回上次 SELECT 或 SHOW 查询找到的并且等待读取的结果行数。要从 getNumRows() 获取结果,您必须在查询**之后**以及在检索任何结果**之前**运行它。要检索 INSERT、UPDATE、REPLACE 或 DELETE 查询影响的行数,请使用 getAffectedRows()
。
. . .
$db->setQuery($query);
$db->execute();
$num_rows = $db->getNumRows();
print_r($num_rows);
$result = $db->loadRowList();
将返回
3
getNumRows()
仅对返回实际结果集的语句(如 SELECT 或 SHOW)有效。如果您在 loadRowList()
(或任何其他检索方法)之后运行 getNumRows()
,您将收到 PHP 警告
Warning: mysql_num_rows(): 80 is not a valid MySQL result resource
in libraries\joomla\database\database\mysql.php on line 344