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
carefully... while ($result
carefully...
while ($result = db_fetch_object($result)) {
echo $result;
}
you are using the same variable...
try this code
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)
Only use db_result()
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...
Looks like you maybe need a PHP and MySQL primer.. ;-)
gpk
----
www.alexoria.co.uk
gpk
----
www.alexoria.co.uk
Ok, but multiple...
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:
In normal php I would select a table with mysql_query() and then just say something like:
By the way I really appreciate the help on something so basic.
Help
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.
<?php$sql = 'SELECT
http://www.trailheadinteractive.com
http://api.drupal.org/api/gro
See also http://api.drupal.org/api/group/database/6.
gpk
----
www.alexoria.co.uk
gpk
----
www.alexoria.co.uk