2013-09-22 62 views
0

我有一個名爲「指數」,與2排它表:標題和正文mysql_result()不工作

代碼:

<?php 
$con = mysql_connect("localhost", "elenbyin_vadim", "passr422"); 

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

$db_selected = mysql_select_db("elenbyin_vadim", $con); 

$sql = "SELECT * from index"; 
$result = mysql_query($sql,$con); 

echo mysql_result($result,0); 

mysql_close($con); 
?> 

但我發現了一個惱人的錯誤: 警告:mysql_result()期望參數1是資源,布爾在第14行給出/home/elenbyin/public_html/elenby.co.il/vadim/tryout.php

我不知道該怎麼辦不管我嘗試過什麼 - 都行不通! 我該如何解決這個問題並從表格中獲取信息?

+1

拋棄mysql_ *現在。遲早你將不得不因爲他們將被從語言中刪除。 http://stackoverflow.com/questions/13944956 – TecBrat

+0

@TecBrat如果你要傳播有關mysql_ *,請鏈接到http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions -in-php/12860046#12860046 – BLaZuRE

+0

@BLaZuRE這兩個鏈接都適用於表達這一點。我可以向你保證,下次我想發佈一個類似的鏈接時,我不會記得以前使用過哪一個,也不知道你建議使用哪一個。 :-) – TecBrat

回答

4

有保留MySQL的關鍵字嘗試使用反單引號角落找尋表名在查詢index問題

SELECT * from `index` 

如果您嘗試呼應的錯誤,你會得到

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 'index

Reserved Keywords

0

錯誤消息告訴你,$result===false這意味着你的查詢沒有執行。我打算說「檢查你的憑據」,但另一個答案指出你有一個關鍵字作爲列名。解決這個問題,你會好起來的,但請看看我提出的問題。

0

你的查詢有一個錯誤,你可以添加

if (!$result) { 
die('Could not query:' . mysql_error()); 
} 

看到它。

0

INDEX是一個mysql保留關鍵字。

你應該首先檢查你的查詢有返回任何記錄或不

$sql = "SELECT * from index"; 
$result = mysql_query($sql,$con); 
if($result) 
{ 
echo mysql_result($result,0); 
}