-4

i want to write a php variable inside a html option tag, but my code is not working.

<html>

<body>
    <?php $host="localhost" ; 
    $mysql_db="db" ; 
    $mysql_u="root" ; 
    $mysql_p="" ; 
    mysql_connect( "$host", "$mysql_u", "$mysql_p"); 
    mysql_select_db( "$mysql_db"); 
    $sel="select * from site" ; 
    $val=mysql_query($sel);
    while($row=m ysql_fetch_array($val, MYSQL_ASSOC)) 
    { 
            $a=$row[ 'a'];
            <option value="$a" name="a">$a</option>
    } ?>
</body>

</html>

i saved this file in .html extn is it right?

user3763227
  • 292
  • 2
  • 15

2 Answers2

1

You should echo it:

echo '<option value="'.$a.'" name="a">'.$a.'</option>';

And concatenate your vars.

Also, don't use the mysql extension, either use the mysqli or PDO extensions and prepared statements. Here's why: Why shouldn't I use mysql_* functions in PHP?.

Community
  • 1
  • 1
arielnmz
  • 8,354
  • 9
  • 38
  • 66
0

first print the select

echo '<select>';
......
......

 while($row=m ysql_fetch_array($val, MYSQL_ASSOC)) 
    { 
            $a=$row['a'];
            echo "<option value=".$a." name='a'>".$a."</option>";
    } ?>
....
...
echo '</select>';
ɹɐqʞɐ zoɹǝɟ
  • 4,342
  • 3
  • 22
  • 35