我试图获得一个下拉列表来显示包含国家('level_4')的列数据,但将主键的值'id')进行表单提交。我正在从openoffice基础表单迁移,所以我已经写了一个可用的sql查询。这是我尝试迁移到一个webform,并且我在PHP语法方面遇到困难。试图用PHP和MySQL填充下拉列表,mysql_fetch_assoc()错误
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>
什么也没有发生在Web表单上,但我得到的源代码以下消息:
<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>
我在做什么错?
表不是空的吗?试试mysql_num_rows()来检查结果。 – Damien 2011-05-20 13:28:25