2011-05-20 70 views
0

我試圖獲得一個下拉列表來顯示包含國家('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> 

我在做什麼錯?

+0

表不是空的嗎?試試mysql_num_rows()來檢查結果。 – Damien 2011-05-20 13:28:25

回答

0

我覺得你的報價是錯誤的。指定你的數據庫表和列或沒事的時候您應該使用反引號('):

45  $sql="SELECT `level_4`,`id` FROM `mtmg`.`geography`"; 
46  $result=mysql_query($sql, $connection); 

如果不解決這個問題,嘗試創建數據庫連接,然後就確保有東西來了你的結果:

47  die("<pre>".print_r($result)."</pre>"); 

希望有所幫助。

+0

This works too! – user81997 2011-05-20 13:31:23

1

錯誤是說你的查詢不好。更改

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

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

完美,非常感謝! – user81997 2011-05-20 13:29:46