我的SQL语法出错

时间:2013-06-30 15:08:39

标签: php mysql

所以,我整天都在打这个代码。我尝试了很多东西,但没有用。 因此,我来​​这里寻求答案。

编辑:我修复了评论中提到的一些问题。然而,问题仍然存在。 错误是:

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());

5 个答案:

答案 0 :(得分:0)

省略其中一个“where”:

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

有许多单词不能作为列,表,视图或任何查询中的任何名称而不采取预防措施让mysql正确解释它们,看here

valuesname是其中两个。

答案 1 :(得分: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指出的那样。

答案 2 :(得分: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

答案 3 :(得分:0)

VALUES是一个MySQL关键字,在用作名称时需要加入反引号:

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

答案 4 :(得分:0)

VALUES是mysql保留关键字,你需要用这样的反引号将其括起来

     SELECT * FROM `values` where

mysql reserved keywords