-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?

3
  • use echo like, echo '<option value="'.$a.'" name="a">'.$a.'</option>'; Commented Jun 21, 2014 at 16:17
  • I'm sure this has been answered several times in several sites. This site is not a debugging service. Commented Jun 21, 2014 at 16:36
  • Saved it as a html file?? I think you should start learning the basics first. There are a huge amount of examples to find Commented Jun 21, 2014 at 21:38

2 Answers 2

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?.

Sign up to request clarification or add additional context in comments.

4 Comments

Don't forget the mysql deprecation and removal.
sorry i can't get the output i saved this file in .html extn is it right?
The file's extension must be .php, you have to setup an http server with php support.
Thanks now its working i changed my file extn as .php
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>';

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.