select语句中的Mysql语法错误

时间:2016-12-26 06:29:07

标签: php mysql mariadb

我有一个结构为

的表main_tag
1   id int(100) 
2   name varchar(100)   
3   description varchar(1000)
4   added_on timestamp  

并具有如下的PHP功能

function all_data_of_main_tag_table(){

        include_once 'db.php';
        $conn = new mysqli(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
        $json = array();
        $nameQuery ='SELECT * FROM `main_tag` WHERE 1';
        echo '<br>'.$nameQuery.'<br>';
        $rsnameQuery = $conn->query($nameQuery);      
        if($rsnameQuery === false){
            echo 'hi'.'<br>';
            trigger_error('Wrong SQL: '.$nameQuery.' Error: '.$conn->error, E_USER_ERROR);
        }
        else{
           $rows_returned = $rsnameQuery->num_rows;
        }
        while($row = $rsnameQuery->fetch_assoc()){
            $json =$row;
        }
        $conn->close();
        return $json;
    }

运行此功能时出错:

{
  "description": null,
  "name": null,
  "id": null
}
MySQL error: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '' at line 1

2 个答案:

答案 0 :(得分:2)

在您的查询中,您未指定查找位置

Where 1

它没有指定任何列

// it should be something like this 
Where id = 1

所以你的查询就像这样

$nameQuery ='SELECT * FROM `main_tag` WHERE id = 1

答案 1 :(得分:0)

我的错误我的查询没问题,错误是其他查询。然而

while ($row = mysqli_fetch_assoc($rsnameQuery)) {
   $json[] = $row;
}

这是写而不是

while($row = $rsnameQuery->fetch_assoc()){
     $json =$row;
}

这不会返回数据。