简体   繁体   English

用PHP填充数据库中的选择

[英]Populate select from database with php

Something is wrong with that code, i mean, it's not doing what i want it to. 该代码出了点问题,我的意思是,它没有按照我想要的去做。

<?php
     $con = mysql_connect($db_s, $db_u, $db_p);
     $db = mysql_select_db("saritur", $con);
     $query = "SELECT resumo FROM resumos";
     $res = mysql_query($query,$con);

     echo "<select name='usuario' id='usuario'>";
     while (($row = mysql_fetch_row($res)) != null)
     {
     echo "<option value = '".$row['resumo']."'>".$row['resumo']."</option>";
     }
     echo "</select>";
     mysql_close($con);
?>

I'm new into PHP so that's already a problem, i got that code from another question in here but still won't work 我是PHP的新手,所以这已经是一个问题,我从另一个问题中获得了该代码,但仍然无法正常工作

Edit: 编辑:

Ok guys i had a stupid problem, the variables $db_s, $db_u etc was on a include file, unfortunately i was not getting the values from it so i had to declare them on the main file and it's looking like this now: 好的,我有一个愚蠢的问题,变量$ db_s,$ db_u等位于包含文件中,不幸的是我没有从中获取值,所以我不得不在主文件上声明它们,现在看起来像这样:

<select name="resumo" id="usuario">
   <?php
   $db_u = 'phiter';
   $db_p = '****';
   $db_s = '127.0.0.1';

   $con = mysql_connect($db_s, $db_u, $db_p);
   $db = mysql_select_db("saritur", $con);
   $query = "SELECT * FROM resumos";

   $res = mysql_query($query,$con);
       while ($row = mysql_fetch_row($res))
       {
           echo "<option value = '{$row['resumo']}'>{$row['resumo']}</option>";
       }

    mysql_close($con);
    ?>
</select>

It is communicating with the database and the number of rows are the same as the number on the database, NICE! 它正在与数据库进行通信,并且行数与数据库上的行数NICE相同! But it's all showing white blocks, there is nothing written on the options. 但这全都显示白色块,选项上没有任何内容。 I saw the error on page code, it says 我看到页面代码上的错误,它说

Notice: Undefined index: resumo in xxx on line 48 注意:未定义的索引:第48行的xxx中的恢复

WHYYYYYY WHYYYYYY

Another update: 另一个更新:

Changed mysql_fetch_row into mysql_fetch_array , now it works mysql_fetch_row更改为mysql_fetch_array ,现在可以使用

I'm going to have a gamble here that this is the only thing that is wrong... 我将在这里赌博,这是唯一的错误...

while (($row = mysql_fetch_row($res)) != null)

change to 改成

while (($row = mysql_fetch_assoc($res)))

Why? 为什么? See the reference pages 请参阅参考页

http://php.net/manual/en/function.mysql-fetch-row.php http://php.net/manual/en/function.mysql-fetch-row.php

http://php.net/manual/en/function.mysql-fetch-assoc.php http://php.net/manual/en/function.mysql-fetch-assoc.php

mysql_fetch_row() returns a numerical array, whereas mysql_fetch_assoc returns a keyed array... which is how you're using it. mysql_fetch_row()返回一个数字数组,而mysql_fetch_assoc返回一个键控数组...这就是您使用它的方式。

Alternatively you can keep using mysql_fetch_row() but then use $row[0] or reset($row) to access the first value. 另外,您可以继续使用mysql_fetch_row(),然后使用$ row [0]或reset($ row)访问第一个值。

使用mysql_fetch_assoc或mysql_fetch_array代替mysql_fetch_row

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM