/**
* 1 -
* $type (, )
*/
$result = db_query( "SELECT nid, title FROM node WHERE type = '$type'" );
$items = array();
while ($row = db_fetch_object($result)) {
$items[] = l($row->title, "node/{$row->nid}" );
}
return theme( 'item_list' , $items);
$result = db_query( "SELECT n.nid, n.title FROM {node} n WHERE n.type = '$type'" );
$type
is story' UNION SELECT s.sid, s.sid FROM {sessions} s WHERE s.uid = 1/*
, then the whole query will be like this:
SELECT n.nid, n.title FROM {node} n WHERE n.type = 'story' UNION SELECT s.sid, s.sid FROM {sessions} s WHERE s.uid = 1/*'
db_query( "SELECT n.nid FROM {node} n WHERE n.nid > %d" , $nid);
db_query( "SELECT n.nid FROM {node} n WHERE n.type = '%s'" , $type);
db_query( "SELECT n.nid FROM {node} n WHERE n.nid > %d AND n.type = '%s'" , $nid, $type);
db_query( "SELECT n.nid FROM {node} n WHERE n.type = '%s' AND n.nid > %d" , $type, $nid);
LIKE %monkey%
)IN (... , ... , ...)
constructions, use the db_placeholders () function, which will create the necessary sequence of substitutes, using a given array of parameters, for example:
$nids = array(1, 5, 449);
db_query( 'SELECT * FROM {node} n WHERE n.nid IN (' . db_placeholders($nids) . ')' , $nids);
If you are using the Devel module, you have a very simple way of getting final queries for debugging purposes. Just call thedb_queryd()
function with exactly the same parameters as you calldb_query()
.
$result = db_query( "SELECT n.nid, n.title FROM {node} n WHERE n.type = '%s'" , $type);
SELECT n.nid, n.title FROM {node} n WHERE n.type = '%s' LIMIT 0, 10
OFFSET 0 LIMIT 10
construct. And on some Orakle, the syntax is different again. What to do?
// 10
$result = db_query_range( "SELECT n.nid, n.title FROM {node} n WHERE n.type = '%s'" , $type, 0, 10);
db_query_range()
by the presence of just one optional parameter, which you can read about on the documentation page. With this function, flipping pages is as simple as twice two:
/**
* 2 - ,
*/
//
$result = pager_query( "SELECT n.nid, n.title FROM {node} n WHERE n.type = '%s'" , $type, 0, 10);
$items = array();
while ($row = db_fetch_object($result)) {
$items[] = l($row->title, "node/{$row->nid}" );
}
$output = theme( 'item_list' , $items);
//
$output .= theme( 'pager' );
return $output;
$result = pager_query(db_rewrite_sql( "SELECT n.nid, n.title FROM {node} n WHERE n.type = '%s'" , 'n' , 'nid' ), $type, 0, 10);
// ,
function my_module_db_rewrite_sql($query, $primary_table, $primary_field, $args) {
switch ($primary_field) {
case 'nid' :
if ($primary_table == 'n' ) {
$ return [ 'join' ] = "LEFT JOIN {users} u ON $primary_table.uid = u.uid" ;
$ return [ 'where' ] = 'u.login > ' . time() - 60 * 60 * 24;
}
return $ return ;
break ;
}
}
SELECT n.nid, n.title FROM {node} n LEFT JOIN {users} u ON n.uid = u.uid WHERE n.type = '%s' AND u.login > 199976743
pager_query()
and will be processed as usual.
/**
* 3 - ,
*/
// db_rewrite_sql
$result = pager_query(db_rewrite_sql( "SELECT n.nid, n.title FROM {node} n WHERE n.type = '%s'" , 'n' , 'nid' ), $type, 0, 10);
$items = array();
while ($row = db_fetch_object($result)) {
$items[] = l($row->title, "node/{$row->nid}" );
}
$output = theme( 'item_list' , $items);
$output .= theme( 'pager' );
return $output;
* This source code was highlighted with Source Code Highlighter .
Source: https://habr.com/ru/post/52469/