2013-03-19 17:27
浏览 53


i'm relatively new to this trying to get a mysql database array of Username to be shown in a html drop down input for another form however the php script just keep being shown rather than the function. below is a screenshot of the error

the php script is sat within my html file

$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = '';
$db = 'fid';

$conn = mysql_connect($dbhost,$dbuser,$dbpass);
if (!$conn)
die('Could not connect: ' . mysql_error());


echo '<label class="input" for="investigator" type="input">Importance:</label><select id="investigator" name="investigator">';
$resource = mysql_query("SELECT `Username` FROM `user`");
if($resource && mysql_num_rows($resource)) {
    while ($row = mysql_fetch_assoc($resource)){
        echo '<option value="'.$row['Username'].'">'.$row['Username'].'</option>';
echo '</select>';

I think its an issue within the while loop however cannot fix it and its getting very frustrating!

图片转代码服务由CSDN问答提供 功能建议

我是一个相对较新的尝试获取用户名的mysql数据库数组在html下拉列表中显示 另一种形式的输入,但PHP脚本只是继续显示而不是功能。 下面是错误的屏幕截图

http:// s10 / j4xuamkwp / untitled.png


 &lt;  ?php 
 $ dbhost ='localhost'; 
 $ dbuser ='root'; 
 $ dbpass =''; 
 $ db ='fid'; 
 $ conn = mysql_connect($ dbhost,$  dbuser,$ dbpass); 
if(!$ conn)
mysql_select_db($ db); 
echo'&lt; label class =“input”  for =“investigator”type =“input”&gt;重要性:&lt; / label&gt;&lt; select id =“investigator”name =“investigator”&gt;'; 
 $ resource = mysql_query(“SELECT`Username` FROM`  user`“); 
if($ resource&amp;&amp; mysql_num_rows($ resource)){
 while($ row = mysql_fetch_assoc($ resource)){
 echo'&lt; option value =”'。$ row [  '用户名']。'“&gt;'。$ row ['用户名']。'&lt; /选项&gt;'; 
echo'&lt; / select&gt;'; 
mysql_close($ conn)\  n?&gt; 

我认为这是while循环中的一个问题但是 修复它,它变得非常令人沮丧!

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • douwen5066 2013-03-19 17:29

    Your file needs to have a .php extension to work properly.

    .html will not be recognized as a php file.

    Name your file: myfile.php for files that have php code in them. NOT myfile.html

    EDIT: As others have pointed out, you can add html as a php type, but that's not typically how it's done, nor would I recommend it.

    点赞 打赏 评论
  • dotwc62080 2013-03-19 17:32

    It sounds like your server isn't parsing PHP properly. Couple troubleshooting tips-

    1) make sure you're PHP is enabled on the server.
    2) make sure the extension of your file is ".php".
    3) If you would like your server to parse ".htm|html" files as PHP, you can change your .htaccess (assuming you are using apache), by following these steps.

    点赞 打赏 评论
  • drzdu44226 2013-03-19 17:34

    Yep, .html will not be read by the PHP interpreter (the part of the web server that reads and computes PHP scripts).

    If you rename your file with a .php ending, for example file.php (not file.html) your script should work.

    This part of your script has me confused:

    if (!$conn)
    die('Could not connect: ' . mysql_error());

    Should it not be

    if (!$conn) {
    die('Could not connect: ' . mysql_error());

    The { and } tells PHP which part of your script is if or while and which parts aren't.

    点赞 打赏 评论

相关推荐 更多相似问题