While updating database queries from D6 to D7 in a not fully properly ported and tested Drupal 7 module - this below is an example of a query that leaves an undefinewd variable $nid in this case on line 5 - I have a lot of these kinds of problems with the queries leaving undefined variable errors now becasse the D7 query does not return an associative array any longer - only objects. Is there a proper standard coding example on how to fix ???
The original D6 query was:
if (db_result(db_query("SELECT * FROM {uc_recurring_stripe} WHERE nid = %d", $item['nid']['#value']))) {
I tried to change it to - as line 2 below - but that generates a parse error of unexpected T operator at the fetchAssoc
if (isset($item['nid']['#value'])) {
if (db_query('SELECT * FROM {the_table} WHERE nid = :nid', array(':nid' => $item['nid']['#value'])))->fetchAssoc();
$title = function_name($item['nid']['#value']);
if (isset($title)) {
$form['items'][$key]['desc']['#value'] = l($title, 'node/' . $nid);
I also tried
if (db_query('SELECT * FROM {the_table} WHERE nid = :nid', array(':nid' => $item['nid']['#value']), array(':nid' => $nid))) {
Any iteration of this code creates an undefined variable of $nid at the $form line (5) above
Is the "if"statement GONE from drupal 7 as well and ALL queries must be re-written to foreach loops ??
I realize something like this should work
$nid = 1;
$result = db_query('SELECT n.nid, n.title, n.created FROM {the_table} n WHERE n.nid = :nid', , array(':nid' => $item['nid'] ['#value']));
// Result is returned as a iterable object that returns a stdClass object on each iteration
foreach ($result as $record) {
// Perform operations on $record->title, etc. here.
// in this example the available data would be mapped to object properties:
// $record->nid, $record->title, $record->created
}
BUT . . . is this complication now the ONLY way to do this to define the variable for line 5 from the query ??