|
Posted by Jim Michaels on 01/10/06 21:25
"powerx" <not@withheld.com> wrote in message
news:43b3d661$0$904$db0fefd9@news.zen.co.uk...
> Hi All,
>
> Would have thought this was a FAQ but I'm having trouble googling it so
> any help appreciated...
>
> I have an SQL database
>
> ID BRAND
> 1 Apples
> 2 Pears
> 3 Oranges
> 4 ...
> 5 ...
>
> What is the easiest way of getting the SQL "Brand List" into a dropdown
> menu in my HTML / PHP page. It should automatically update when new items
> are added?
actually, what you have there is not a database, it is a table. a database
is a group of tables, so to speak. This will explain the following code:
make a dbinc.php (or whatever PHP name you wish) that has at least the
following 4 lines in it:
<?php
$link=mysql_connect("myserver.com:3306", "username", "password");
mysql_select_db("my_database", $link);
?>
<?php
include 'dbinc.php';
$q=mysql_query("SELECT ID, BRAND FROM tablename ORDER BY BRAND", $link);
?>
<select name="myselect">
<?php
while ($row=mysql_fetch_array($q)) { ?>
<option value="<?=$row['ID']?>"><?=stripslashes($row['BRAND'])?></option>
<?php
} ?>
</select>
stripslashes() is only necessary if there could be a ' or " in the text.
you probably can take it out, but it doesn't hurt to leave it in. it takes
out the \ that MySQL pushes in when it finds special characters. It is also
necessary to use stripslashes on binary stuff (blob types) as the data comes
out, and mysql_escape_string() as the data comes in. mysql_escape_string()
is also used to prepare the MySQL INSERTs for ' or " characters.
If you would like to learn a lot about PHP programming, I suggest you
download the extended CHM manual from the PHP web site. (12MB). It has
plenty of examples.
Navigation:
[Reply to this message]
|