0
votes

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 ??

1
Of course you'll get $nid is undefined because it's not being defined anywhere! Suggest you set aside an hour and read the docs, this, and the pages around it: drupal.org/node/310072klidifia

1 Answers

0
votes

The question was about CODING STANDARD to declare a variable from converting that query. It was NOT my code originally

I used

     if (isset($item['nid']['#value'])) {
        $nid = [ ];
         $exists = db_query('SELECT 1 FROM {the_table} WHERE nid = :nid', array(':nid' => $item['nid']['#value']))->fetchField();
           if($exists)
            {
            if ($node = menu_get_object()) {
            // Get the nid
            $nid = $node->nid;
            $title = _function_name($item['nid']['#value']);

obviously to declare the variable and make it an object

            $nid = [ ];
            if ($node = menu_get_object()) {
            // Get the nid
            $nid = $node->nid;

The response did not answer the question as to coding standards - I KNEW $nid was not declared - the question was

Is there a proper standard coding example on how to fix ???

I have answered my own question but it does not tell me if this is a proper coding standard - and I had ALREADY read the linked page at https://www.drupal.org/node/310072 and it DID NOT address the question I asked