Whats the difference between these two code snippets (1 works, 2 doesnt) : 1) $nresult = db_query('SELECT n.nid, n.body, n.type, n.title, n.promote, n.moderate, n.teaser, n.created, n.changed, u.name FROM {node} n , {users} u, {term_node} tn, {users_roles} ur where n.uid = u.uid AND n.nid=tn.nid AND u.uid=ur.uid AND n.status = 1 AND n.created > %d ORDER BY n.created', $period); $nodes = array(); while ($node = db_fetch_object($nresult)) { $nodes[$node->nid] = $node; } 2) $nresult = mysql_query('SELECT n.nid, n.body, n.type, n.title, n.promote, n.moderate, n.teaser, n.created, n.changed, u.name FROM node n , users u,term_node tn, users_roles ur where n.uid = u.uid AND n.nid=tn.nid AND u.uid=ur.uid AND n.status = 1 AND n.created > $period ORDER BY n.created'); $nodes = array(); while ($row = mysql_fetch_array($nresult, MYSQL_NUM)) { $nodes[$node->nid] = $node; } Regads, V
VJ Rao wrote:
$nresult = mysql_query('SELECT n.nid, n.body, n.type, n.title, n.promote, n.moderate, n.teaser, n.created , n.changed, u.name FROM node n , users u,term_node tn, users_roles ur where n.uid = u.uid AND n.nid=tn.nid AND u.uid=ur.uid AND n.status = 1 AND n.created > $period ORDER BY n.created');
$period won't be expanded because '$period' is literall $period.
VJ Rao pravi:
Whats the difference between these two code snippets (1 works, 2 doesnt) :
1)
$nresult = db_query('SELECT n.nid, n.body, n.type, n.title, n.promote, n.moderate, n.teaser, n.created, n.changed, u.name FROM {node} n , {users} u, {term_node} tn, {users_roles} ur where n.uid = u.uid AND n.nid=tn.nid AND u.uid=ur.uid AND n.status = 1 AND n.created > %d ORDER BY n.created', $period);
$nodes = array();
while ($node = db_fetch_object($nresult)) { $nodes[$node->nid] = $node; }
2)
$nresult = mysql_query('SELECT n.nid, n.body, n.type, n.title, n.promote, n.moderate, n.teaser, n.created, n.changed, u.name FROM node n , users u,term_node tn, users_roles ur where n.uid = u.uid AND n.nid=tn.nid AND u.uid=ur.uid AND n.status = 1 AND n.created > $period ORDER BY n.created');
You should use " (double quotes) if you want the *value* of $period to be inserted ...
$nodes = array();
while ($row = mysql_fetch_array($nresult, MYSQL_NUM)) { $nodes[$node->nid] = $node;
}
Regads,
V
Tadej and Earl its nice you analyze the sql query but after sql query execution $nodes array will be empty won't it?=) Mac maciej.perlinski@meant4.com -----Original Message----- From: Tadej Baša [mailto:tadej.basa@gmail.com] Sent: Wednesday, September 13, 2006 8:04 PM To: development@drupal.org Subject: Re: [development] programming question (drupal specific) VJ Rao pravi:
Whats the difference between these two code snippets (1 works, 2 doesnt) :
1)
$nresult = db_query('SELECT n.nid, n.body, n.type, n.title, n.promote, n.moderate, n.teaser, n.created, n.changed, u.name FROM {node} n , {users} u, {term_node} tn, {users_roles} ur where n.uid = u.uid AND n.nid=tn.nid AND u.uid=ur.uid AND n.status = 1 AND n.created > %d ORDER BY n.created', $period);
$nodes = array();
while ($node = db_fetch_object($nresult)) { $nodes[$node->nid] = $node; }
2)
$nresult = mysql_query('SELECT n.nid, n.body, n.type, n.title, n.promote, n.moderate, n.teaser, n.created, n.changed, u.name FROM node n , users u,term_node tn, users_roles ur where n.uid = u.uid AND n.nid=tn.nid AND u.uid=ur.uid AND n.status = 1 AND n.created > $period ORDER BY n.created');
You should use " (double quotes) if you want the *value* of $period to be inserted ...
$nodes = array();
while ($row = mysql_fetch_array($nresult, MYSQL_NUM)) { $nodes[$node->nid] = $node;
}
Regads,
V
Maciek Perlinski wrote:
Tadej and Earl its nice you analyze the sql query but after sql query execution $nodes array will be empty won't it?=)
Not empty, actually, but it will only have one record. Good catch, I missed that. In example 2, $nodes[$node->nid] needs to be $nodes[$node[$nid]]
Why are you using mysql_query() in the second, and db_query() in the first one? In Drupal, it is recommended that you use db_query() always.
Actually > In example 2, $nodes[$node->nid] needs to be $nodes[$node[$nid]] should be: $nodes[$node->nid] needs to be $nodes[$node['nid']]
Well no interation=) Anyway was easier to point this place than analyze sql query at 9pm, lol=) -----Original Message----- From: Earl Miles [mailto:merlin@logrus.com] Sent: Wednesday, September 13, 2006 8:19 PM To: development@drupal.org Subject: Re: [development] programming question (drupal specific) Maciek Perlinski wrote:
Tadej and Earl its nice you analyze the sql query but after sql query execution $nodes array will be empty won't it?=)
Not empty, actually, but it will only have one record. Good catch, I missed that. In example 2, $nodes[$node->nid] needs to be $nodes[$node[$nid]]
On 9/13/06, Tadej Baša <tadej.basa@gmail.com> wrote:
You should use " (double quotes) if you want the *value* of $period to be inserted ...
And you should only use that technique if you are certain of the source of the data (e.g. it's not from user input). Otherwise you invite a SQL Injection attack. More details: http://drupal.org/node/62304 Regards, Greg -- Greg Knaddison | Growing Venture Solutions Denver, CO | http://growingventuresolutions.com Technology Solutions for Communities, Individuals, and Small Businesses
Is that the complete snippet? //-------------------------------------------------------------------------- -- $nresult = mysql_query('SELECT n.nid, n.body, n.type, n.title, n.promote, n.moderate, n.teaser, n.created , n.changed, <http://u.name/> u.name FROM node n , users u,term_node tn, users_roles ur where n.uid = u.uid AND n.nid=tn.nid AND u.uid=ur.uid AND n.status = 1 AND n.created > $period ORDER BY n.created'); $nodes = array(); while ($row = mysql_fetch_array($nresult, MYSQL_NUM)) { $nodes[$node->nid] = $node; } //-------------------------------------------------------------------------- ------------------------------------------ If so in the while statement : $nodes[$node->nid] = $node; has no sense because you are receiving an assoc array and keep it in $row array which has no connection to $nodes[$node->nid]=$node; What is $node??? What is $node->nid the only thing which contains any data is $nresult and $row so check what your variables contain. Mac maciej.perlinski@meant4.com _____ From: VJ Rao [mailto:cmsconsultant@gmail.com] Sent: Wednesday, September 13, 2006 7:33 PM To: development@drupal.org; support@drupal.org Subject: [development] programming question (drupal specific) Whats the difference between these two code snippets (1 works, 2 doesnt) : 1) $nresult = db_query('SELECT n.nid, n.body, n.type, n.title, n.promote, n.moderate, n.teaser, n.created, n.changed, u.name <http://u.name/> FROM {node} n , {users} u, {term_node} tn, {users_roles} ur where n.uid = u.uid AND n.nid=tn.nid AND u.uid=ur.uid AND n.status = 1 AND n.created > %d ORDER BY n.created', $period); $nodes = array(); while ($node = db_fetch_object($nresult)) { $nodes[$node->nid] = $node; } 2) $nresult = mysql_query('SELECT n.nid, n.body, n.type, n.title, n.promote, n.moderate, n.teaser, n.created , n.changed, u.name <http://u.name/> FROM node n , users u,term_node tn, users_roles ur where n.uid = u.uid AND n.nid=tn.nid AND u.uid=ur.uid AND n.status = 1 AND n.created > $period ORDER BY n.created'); $nodes = array(); while ($row = mysql_fetch_array($nresult, MYSQL_NUM)) { $nodes[$node->nid] = $node; } Regads, V
participants (7)
-
Earl Miles -
Greg Knaddison - GVS -
Khalid B -
Maciek Perlinski -
Steve Ringwood -
Tadej Baša -
VJ Rao