db_query issue

Smontje - February 27, 2008 - 10:26

Hello,

I actually have a rather "stupid" problem but i cant get it solved. I have in my database a table wich is called country and where all the countries are kept in with their iso code and name. I wish to read all the data from that table so i can fill up my option value in html code.

The way i think to do it:

"This is done at the top of the page"

<?php

$results
= db_query("SELECT * FROM country");
?>

"This is what i try to do at my html code"

<?php
           
for($i = 0; $i < db_num_rows($results); $i++)
        {                                               
            
$result = mysql_result($results, $i);
                     echo
"<option value=" . $xxxxx. " $selected>" .   $xxxxxxx . "</option>";
        }               
?>

As you can see im a bit confused what to do with my option value's and this for a simple reason. When i do echo sizeof($results) i get 1 but when i do db_num_rows i get 239. So i wonder if i only got the first column from my database? And suppose i get all the data correctly how do i tell drupal that i want for example the second column of data in my array to be my option value and the third column to be the name that gets displayed as a option.

I hope this information is sufficient for solving my problem.

Thx in advance

Assuming: your table name is

clivesj - February 27, 2008 - 11:05

Assuming:
your table name is {country} ... always put table name between {} when performing the sql
database column for name = name
database column for code = iso_code

<?php
                    
$results
= db_query("SELECT * FROM {country}");
  while (
$result = db_fetch_array($results)) {
    echo
'Country name = ' . $result[]['name'] . 'Country code = ' . $result[]['iso_code'];
}
?>

warning: UNTESTED!

Thank you clivesj i will

Smontje - February 27, 2008 - 11:32

Thank you clivesj i will give it a try

Edit:

<?php
while ($result = db_fetch_array($results)) {
echo
"<option value=" . $result[]['iso'] . ">" . $result[]['name'] . "</option>";}           
?>

I echo'ed it your code and it works but the problem is that i cant get this value echo'ed it shows nothing...

sorry, try this first just

clivesj - February 27, 2008 - 11:42

sorry, try this first just to verify the query results can be displayed:

<?php
echo $result[iso]  . " / "  . $result[name] ;}
 
?>

just to see if the names are echoed.
And then, are you trying th output an itemlist or so? I don't get what you are trying to display

Well to clearify: I want to

Smontje - February 27, 2008 - 11:52

Well to clearify:

I want to have a list selection where people can choose from wich country they are from on my custom registration page. Therefore i want that the names of every country that is listed in my database to show up on my list.

Does the code above echo

clivesj - February 27, 2008 - 12:07

Does the code above echo your db results?

Yes it does it correctly

Smontje - February 27, 2008 - 14:21

Yes it does it correctly, so im guessing my error is when trying to echo my option value's?

Edit: It finally worked thank you !!!!!!

 
 

Drupal is a registered trademark of Dries Buytaert.