$query=db_query('SELECT title FROM {node} WHERE type = \'new_boats\' ');
 $categories=Array('');
 while($row=db_fetch_array($query)){
   $categories[]=$row['title'];
   echo $row['title'];
 }

Can anyone suggest what's wrong with the above code?

I had something similar working under D6, but I'm getting just a blank page.

The above code is in a block.

http://localhost/bmarine/#overlay=?q=admin/structure/block/manage/block/...

Comments

Jaypan’s picture

In D6, you weren't supposed to directly insert values into queries. In D7, you cannot:

 $query=db_query('SELECT title FROM {node} WHERE type = :new_boats', array(':new_boats' => 'new_boats'));

Read more about the Database API here: https://drupal.org/node/310069

davecoventry’s picture

Thanks, Jaypan.

davecoventry’s picture

I have altered my code:

  $result=db_query('SELECT nid, title FROM {node} WHERE type = :new_boats', array(':new_boats' => 'new_boats'));
  echo "~~~~~~~~~~~~Check~~~~~~~~~~~~~~~";
  print_r($result);
  if ($result) {
    while ($row = $result->fetchAssoc()) {
      $Titles[]=$row['title'];]
      $nids[]=$row['nid'];]
      print $row['title']."<hr>";
    }
  }

The page is almost rendered as expected, but the code is not executed.

There is no "~~~~~~~~~~~~Check~~~~~~~~~~~~~~~", no print_r($result) and the node's title isn't printed either.

Jaypan’s picture

Well, you've given us no context as to where your code lies, what it is doing, and what it is supposed to be doing, so it's pretty hard to give you a relevant answer.

But, you almost never print things in Drupal, you return them. Which is probably why you aren't seeing anything.

davecoventry’s picture

I think I said in the opening post that it's in a Block. I even gave the URL. :)

This will display the contents of $result.

  $result=db_query('SELECT nid, title FROM {node} WHERE type = :new_boats', array(':new_boats' => 'new_boats'));
  print_r($result);

This doesn't.

  $result=db_query('SELECT nid, title FROM {node} WHERE type = :new_boats', array(':new_boats' => 'new_boats'));
  print_r($result);

  if ($result) {
    while ($row = $result->fetchAssoc()) {
      $Titles[]=$row['title'];]
      $nids[]=$row['nid'];]
      print $row['title'];
    }
  }

The if($result) block appears to suppress not only the printing of the Title, but also the print_r($result).

The only reason I'm printing the output is to make sure that the arrays have the values I'm expecting.

davecoventry’s picture

Thanks again for your help, Jaypan.

It seems to have come together as expected.

I think I had a typo in my code :blush:.

Thank you!

~ Dave

Jaypan’s picture

I think I said in the opening post that it's in a Block. I even gave the URL. :)

So you did! Apologies :)