2013-06-30 38 views
0

所以,我一直在爲這個代碼整天奮戰。我已經嘗試了很多東西,無濟於事。爲此,我來這裏尋求答案。我的SQL語法錯誤

編輯:我修復了評論中提到的一些問題。然而,問題仍然存在。 錯誤是:

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 
'values where `item_id` = 'Throne' ORDER BY `timestamp` DESC LIMIT 10' 
at line 1 

這是我的PHP代碼:

include("config.php"); 
    include("functions.php"); 
       if(isset($_GET['name'])){ 
        $id = mysql_real_escape_string($_GET['name']); 


         $get_rares = mysql_query("SELECT * FROM rares WHERE `name` = '".$id."'") or die(mysql_error()); 
    $rare = mysql_fetch_array($get_rares); 
        if(mysql_num_rows($check) == 0){ 
         echo 'The rare '.$id.' doesn\'t exist!<br>'; 





        }else{ 
         $r = mysql_fetch_array($check); 
         $ids = $r["id"]; 
         $name = $r["name"]; 
         $value = $r["value"]; 
         $lastedited = $r["lastedited"]; 
         $catid = $r["catid"]; 
         $desc = $r["desc"]; 
         $image = $r["image"]; 
         $big_image = $r["big_image"]; 
         $release_value = $r["release_value"]; 
         $releasedate = $r["releasedate"];      
        } 
       }else{ 
        echo 'No rare has been selected to view.<br><br>Click <a href="members.php">here</a> to go to the rare list.'; 
       } 
       ?>   
       <?php $values = mysql_query("SELECT * FROM values where `item_id` = '".$id."' ORDER BY timestamp DESC LIMIT 10") or die(mysql_error()); 
+0

關鍵字和錯誤是什麼? – Ryan

+0

什麼是錯誤輸出? –

+0

添加了錯誤輸出。我知道我忘了一些東西! – user2536680

回答

0

name是一個字符串,你需要引用它。

$get_rares = mysql_query("SELECT * FROM rares WHERE name = '$id'") or die(mysql_error()); 

$values = mysql_query("SELECT * FROM values WHERE item_id = '$id' ORDER BY timestamp DESC LIMIT 10") or die(mysql_error()); 

WHERE也寫了兩次,因爲@Axel指出。

0

省略的 「在那裏」 的一個:

"SELECT * FROM rares where WHERE name = ".$id."" 
//     *********** 

和:

有許多單詞不能被視爲列,表,視圖或任何查詢中的任何名稱w/o採取預防措施有mysql inte正確翻譯它們,看看here

valuesname是其中的兩個。

0

在MySQL中,VALUES是一個關鍵字,你不能用是作爲一個現場或表名,而不把它蜱報價:

SELECT * FROM values where `item_id` = '".$id."' ORDER BY timestamp DESC LIMIT 10 

應該

SELECT * FROM `values` where `item_id` = '".$id."' ORDER BY `timestamp` DESC LIMIT 10 
0

VALUES是一個MySQL關鍵字,並且在用作名稱時需要加反引號:

SELECT * FROM `values` WHERE `item_id` = '$id' ORDER BY timestamp DESC LIMIT 10