I'm wondering if my lack of drupal skills is equal to my lack of ability to find this specific answer in the forums. Sorry if this info is somewhere else...

I'm creating a custom page within drupal using PHP code. I want to pull simple data from the database. For example:

$sql = "SELECT TITLE FROM links";
$result = db_result(db_query($sql));
echo $result;

I have this. It works ok. I just can't figure out how to use the database API to loop through the rest of the records. Something like:

$sql = "SELECT TITLE FROM links";
$result = db_result(db_query($sql));
while ($result = db_fetch_object($result)) {
echo $result;
}

I'm clearly missing some part of this logic. I just want to loop through a simple database pulling a specific result. I've seen other snippets of code, but they are all far more complicated than I need each time.

Any help?

Comments

pedrofaria’s picture

carefully...

while ($result = db_fetch_object($result)) {
echo $result;
}

you are using the same variable...

try this code


$sql = "SELECT TITLE FROM links";
$result = db_result(db_query($sql));
while ($row = db_fetch_object($result)) {
  var_dump($row);
}

cya

--
Pedro Faria de Mirando Pinto
http://www.phpavancado.net - DevBlog (pt_BR)
http://www.eusouopedro.com - Blog (pt_BR)
irc://irc.freenode.org/drupal-br - IRC Help channel (pt_BR)

gpk’s picture

db_result() http://api.drupal.org/api/function/db_result/6 actually gets the result (when it is a single field)...

Also Drupal coding standards would prefer...

$sql = 'SELECT title FROM {links}';
$results = db_query($sql); // Run the query
while ($title = db_result($results)) { // Get the next result, which will/must always be a single field
  echo $title;
}

Looks like you maybe need a PHP and MySQL primer.. ;-)

gpk
----
www.alexoria.co.uk

xpersonas’s picture

Ok, that's awesome. Thank you so much.

What would be the proper way to iterate through multiple results of a row, or even just individually call them by field name:

<?php
   $sql = 'SELECT title, name, type FROM {links}';
   //what would change here//
   ?>

In normal php I would select a table with mysql_query() and then just say something like:

<?php
   $row = mysql_fetch_assoc($table);

   // and then use a loop like this
   do {
   echo $row['title'];
   } while ($row = mysql_fetch_assoc($table));
?>

By the way I really appreciate the help on something so basic.

xpersonas’s picture

Anyone?

I just need to know how to loop through the rows of the result query so that I can echo out multiple fields from that row. I'm just not sure of the syntax for that in drupal.

Any help would be awesome.

criznach’s picture

$sql = 'SELECT title,url FROM {links}';  // Add more fields here or use * for all
$results = db_query($sql); // Run the query
while ($fields = db_fetch_array($results)) { // Get the next result as an associative array
  echo "<pre>"; // Wrap each record in a pre tag for clarity
  foreach($fields as $key => $value) { // Iterate over all of the fields in this row
    echo "$key = $value\n";
  }
  echo "</pre>";
}
gpk’s picture