Retrieving Query Results
There are various ways to retrieve query results depending on whether you need a single row
or the whole result set or whether you are planning to get a range of results for internal use or
for display as a paged result set.
Getting a Single Value
If all you need from the database is a single value, you can use db_result() to retrieve that
value. Here is an example of retrieving the total number of users who have not been blocked
by the administrator (excluding the anonymous user):
$count = db_result(db_query('SELECT COUNT(uid) FROM {users} WHERE status = 1
AND uid != 0'));
Getting Multiple Rows
In most cases, you will want to return more than a single field from the database. Here is a
typical iteration pattern for stepping through the result set:
$type = 'blog';
$status = 1; // In the node table, a status of 1 means published.
$sql = "SELECT * FROM {node} WHERE type = '%s' AND status = %d";
$result = db_query(db_rewrite_sql($sql), $type, $status);
while ($data = db_fetch_object($result)) {
$node = node_load($data->nid);
print node_view($node, TRUE);
}
The preceding code snippet will print out all published nodes that are of type blog (the
status field in the node table is 0 for unpublished nodes and 1 for published nodes). We will
cover db_rewrite_sql() shortly. The db_fetch_object() function grabs a row from the result
set as an object. To retrieve the result as an array, use db_fetch_array(). The practice of
retrieving rows as objects, as opposed to arrays, is common since most developers prefer its
less verbose syntax.
Getting a Limited Range of Results
As you might guess, running the preceding query on a site with, say, 10,000 blog entries is a
dangerous idea. We’ll limit the result of this query to only the ten newest blog entries:
$type = 'blog';
$status = 1; // In the node table, a status of 1 means published.
$sql = "SELECT * FROM {node} n WHERE type = '%s' AND status = %d ORDER BY
n.created DESC";
$result = db_query_range(db_rewrite_sql($sql), $type, $status, 0, 10);
while ($data = db_fetch_object($result)) {
$node = node_load($data->nid);
print node_view($node, TRUE);
}
94 CHAPTER 5 n WORKING WITH DATABASES
Instead of passing the query to db_query() and using the LIMIT clause, we instead use
db_query_range(). Why? Because not all databases agree on the format of the LIMIT syntax, so
we need to use db_query_range() as a wrapper function.
Note that you pass the variables that will fill placeholders before the range (so the type
and status are passed before 0 and 10 in the example just shown).
Getting Results for Paged Display
We can present these blog entries in a better way: as a page of formatted results with links to
more results. We can do that using Drupal’s pager (see Figure 5-2). Let’s grab all of the blog
entries again, only this time we’ll display them as a paged result, with links to additional pages
of results and “first” and “last” links at the bottom of the page.
$type = 'blog';
$status = 1;
$sql = "SELECT * FROM {node} n WHERE type = '%s' AND status = %d ORDER BY
n.created DESC";
$pager_num = 0; // This is the first pager on this page. We number it 0.
$result = pager_query(db_rewrite_sql($sql), 10, $pager_num, NULL, $type,
$status);
while ($data = db_fetch_object($result)) {
$node = node_load($data->nid);
print node_view($node, TRUE);
}
// Add links to remaining pages of results.
print theme('pager', NULL, 10, $pager_num);
Although pager_query() is not really part of the database abstraction layer, it is good to
know when you need to create a paged result set with navigation. A call to theme('pager') at
the end will display the navigation links to the other pages. You don’t need to pass the total
number of results to theme('pager'), because the number of results is remembered internally
from the pager_query() call.
分享到:
相关推荐
【HPW2007韩语阅读器】是一款专门针对HWP格式文件设计的阅读工具,主要用于帮助用户查看和理解韩语文档内容。HWP格式是韩文处理软件Hanword(原名HWP,由韩国翰宇软件开发)所使用的默认文件格式,广泛应用于韩国的...
多元曲线matlab代码Hpw-to-Talk-More-Generative-Models 2020 年 12 月 26 日 我很欣赏评论。 给我发电子邮件! 雇用我! :smiling_face_with_smiling_eyes: 我们一直在谈论通过将高斯分布拟合到每个类来构建分类器...
"hpw:实验室工作自动检查器"是一款专为How.Programming.Works项目设计的工具,旨在简化实验室作业的检查过程。这款工具的核心是利用JavaScript语言编写的,因此对JavaScript的深入理解和应用至关重要。 首先,我们...
matlab code for Hybrid Photovoltaic and Wind power system
HP驱动1005
HP 1005打印机驱动,安装简单易懂,操作方面
风机和光伏联合仿真,与电网并联运行,含有线路参数并且设置完整,有光伏并网逆变器和风机并网逆变器
可用于百度网盘下载大容量资源,破解百度限速。每秒下载速度可达到8Mb.
以SBA-15为载体负载磷钨酸(HPW),用于催化四氢呋喃(THF)开环聚合,采用在线反应红外分析仪(ReactIR)测定THF转化率,实验考察了HPW负载质量百分比、3-氨丙基三乙氧基硅烷(APTES)/SBA-15质量比和载体SBA-15...
γ-氨丙基三乙氧基硅烷修饰的硅胶负载磷钨酸(HPW/M-SiO2)催化剂催化合成扁桃酸甲酯。运用IR,XRD和n-C4H9NH2电位滴定法测试技术表征HPW/M-SiO2催化剂,考察了催化剂用量、物料配比、反应温度和反应时间等对扁桃酸...
模拟结果显示,该极化转换器能在波长为155nm处实现99.9%的极化转换效率(Polarization conversion efficiency, PCE),插入损耗(Insertion loss, IL)为0.82dB,消光比(Extinction ratio, ER)为30.4dB,转换长度...
通过优化HPW结构中介质夹层的厚度、多模部分的宽度和长度,特别是通过调整输入和输出波导的位置,得到了在1550纳米波长下,最大相位误差为0.7度,传输不平衡为0.6分贝,CMRR(共模抑制比)低于-20分贝的HPW混合器。...
4. `hpw8300a.cab`:另一个 cabinet 文件,可能包含了主驱动程序的安装文件,比如设备的驱动程序主体、配置信息等。 5. `hpw8300a.inf`:这是驱动程序的安装信息文件,指导Windows如何安装和配置驱动程序。它包含了...
LCDTIM1中涉及VFP(前同步脉冲)、VBP(背同步脉冲)、VPW(脉冲宽度)和VHDLY(垂直延迟),而LCDTIM2包含HBP(水平前同步脉冲)、HFP(水平后同步脉冲)和HPW(水平脉冲宽度)。这些值应根据具体液晶屏的数据手册...
该模块的核心在于HPW7095和HPW7095A这两款多通道PWM(脉宽调制)控制集成电路。它们被设计用来优化便携式设备的电源效率,尤其适用于便携式数码相机、摄像机和手持仪器等对电源需求敏感的设备。HPW7095/A芯片具备6个...
提出了一种基于混合表面等离子体波导(HPW)的纳米激光器,并对其进行了理论研究和仿真分析。此结构通过金属-介质界面的表面等离子体模式与高增益介质半导体纳米线波导模式耦合,使中间的空气间隙场强得到显著增大,...
通过采用这种方法,HPW能够更快地响应市场需求,提高了产品开发效率,同时提升了客户满意度,有力地推动了企业的发展。 【关键词】 新创企业,精益创业,精益软件开发,敏捷软件开发,看板 本研究论文通过对新创...