Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
Populating drop down with results of query
Old 01-09-2008, 10:15 PM Populating drop down with results of query
Novice Talker

Posts: 7
Trades: 0
Hi;
my first time here, so please be nice...
I am trying to populate a drop down field (part of a form) with the result of my slq query which is like this:
Code:
 
$sql = 'SELECT products_description.products_name '
        . ' FROM products_description LEFT JOIN products_to_categories ON products_description.products_id = products_to_categories.products_id '
        . ' WHERE ( ( ( products_to_categories.categories_id ) = 13 ) ) '
        . ' ORDER BY products_description.products_name ; ';
I never done any PHP coding... but I am modifying existing code to insert this additional field.
Thanks
Veronika
Veronika7747 is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 01-10-2008, 01:56 AM Re: Populating drop down with results of query
mtishetsky's Avatar
King Spam Talker

Posts: 1,226
Name: Mike
Location: Mataro, Spain
Trades: 0
PHP Code:
$sql "...whatever...";
$result mysql_query($sql);
while (
$row mysql_fetch_object($result)) {
   
$rows[] = $row;
}
mysql_free_result($result);
...
foreach (
$rows as $r) {
   echo 
"<option value=\"{$r->id}\">{$r->name}</option>";

Something like that, I guess.
__________________

Please login or register to view this content. Registration is FREE
-
Please login or register to view this content. Registration is FREE
-
Please login or register to view this content. Registration is FREE

And don't forget to give me talkupation!
mtishetsky is offline
Reply With Quote
View Public Profile Visit mtishetsky's homepage!
 
Old 01-10-2008, 02:16 AM Re: Populating drop down with results of query
Novice Talker

Posts: 7
Trades: 0
If you meant this, it does not work for me...
Code:
 <?
$sql = 'SELECT products_description.products_name '
        . ' FROM products_description LEFT JOIN products_to_categories ON products_description.products_id = products_to_categories.products_id '
        . ' WHERE ( ( ( products_to_categories.categories_id ) = 13 ) ) '
        . ' ORDER BY products_description.products_name ; ';

$result = mysql_query($sql); 
while ($row = mysql_fetch_object($result)) { 
   $rows[] = $row; 
} 
mysql_free_result($result); 

for each ($rows as $r) { 
   echo "<option value=\"{$r->id}\">{$r->name}</option>"; 
}  

?>
But perhaps I misunderstood..

Last edited by Veronika7747; 01-10-2008 at 02:18 AM..
Veronika7747 is offline
Reply With Quote
View Public Profile
 
Old 01-10-2008, 02:23 AM Re: Populating drop down with results of query
mtishetsky's Avatar
King Spam Talker

Posts: 1,226
Name: Mike
Location: Mataro, Spain
Trades: 0
Probably you have to read more about working with mysql in php, native documentation is quite fine for it.
__________________

Please login or register to view this content. Registration is FREE
-
Please login or register to view this content. Registration is FREE
-
Please login or register to view this content. Registration is FREE

And don't forget to give me talkupation!
mtishetsky is offline
Reply With Quote
View Public Profile Visit mtishetsky's homepage!
 
Old 01-10-2008, 02:32 AM Re: Populating drop down with results of query
Experienced Talker

Posts: 32
Trades: 0
mysql_free_result only needs to be used if you're concerned about memory and with larger results.

What about using mysql_fetch_rows?
__________________

Please login or register to view this content. Registration is FREE
- Free Online Website Directory - NO Backlinks!
ecuffo is offline
Reply With Quote
View Public Profile Visit ecuffo's homepage!
 
Old 01-10-2008, 03:41 AM Re: Populating drop down with results of query
mtishetsky's Avatar
King Spam Talker

Posts: 1,226
Name: Mike
Location: Mataro, Spain
Trades: 0
I prefer to fetch_objects() since I am used to refering to my data structures as to objects. If you fetch_rows() you wil be unable to call $row->field_name or even $row['field_name'], only $row[0] also having to remember the order of fields in your table and adding new fields very carefully.
__________________

Please login or register to view this content. Registration is FREE
-
Please login or register to view this content. Registration is FREE
-
Please login or register to view this content. Registration is FREE

And don't forget to give me talkupation!
mtishetsky is offline
Reply With Quote
View Public Profile Visit mtishetsky's homepage!
 
Reply     « Reply to Populating drop down with results of query
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.74640 seconds with 12 queries