php - 尝试使用 PHP 和 MySQL 填充下拉列表,mysql_fetch_assoc() 错误

标签 php mysql drop-down-menu

我正在尝试获取一个下拉列表来显示包含国家/地区(“level_4”)的列数据,但采用表单提交的主键(“id”)的值。我正在从 openoffice 基本表单迁移,因此我已经编写了一个有效的 sql 查询。这是我尝试迁移到 Web 表单,但我在 PHP 语法方面遇到了困难。

35<li>
36      <?php 
37      $server="********";
38      $username="********";
39      $password="********";
40      $database="mtmg";
41      
42      $connection = mysql_connect($server, $username, $password) or die('Could not connect'.mysql_error());
43      mysql_select_db($database, $connection) or die("Cannot select db.");
44      
45      $sql="SELECT 'level_4','id' FROM 'mtmg'.'geography'";
46      $result=mysql_query($sql, $connection);
47      
48      echo '<label for="geography">Geography</label>';
49      echo '<select  id="geography" name="geography">';
50      
51      while ($row = mysql_fetch_assoc($result)) {echo '<option value="'.$row['level_4'].'">'.$row['level_4'].'</option>';}
52      echo mysql_error();
53
54      echo '</select>';
55      ?>
56</li>

网络表单上没有任何反应,但我在源代码中收到以下消息:

<li> 
        <label for="geography">Geography</label><select  id="geography" name="geography"><br /> 
<b>Warning</b>:  mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in <b>/f5/user_name/public/index.php</b> on line <b>51/b><br /> 
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''mtmg'.'geography'' at line 1</select>       </li>

我做错了什么?

最佳答案

该错误表明您的查询有问题。改变

$sql="SELECT 'level_4','id' FROM 'mtmg'.'geography'";

$sql="SELECT level_4, id FROM geography";

关于php - 尝试使用 PHP 和 MySQL 填充下拉列表,mysql_fetch_assoc() 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6072495/

相关文章:

PHP 查询 MySQL 函数字符串

php - 这个 MySQL 查询有什么问题?

php - 自动备份数据库的最佳方法

html - 下拉框和组合框有什么区别吗?

css - WordPress 下拉 CSS 不工作

PHP:通过 ID 将 html 内容 append (添加)到现有元素

php - 在 PHP 中将 SQL 查询结果映射到 JSON

mysql - 仅当所有 ID 均适用时才从 ManyToMany 中选择

php - 使用 PHP 和 MySQL 重置数据库

jquery - ASP .Net jQuery DropDownList 更改事件