我正在尝试根据会话用户名显示一个表,但是我收到以下错误。如果它是id
它正在工作但是当它是username
时它不是
SELECT tb_list.* FROM tb_list
WHERE myusername = ".Session::get('username')."
Connection.php第662行中的QueryException:SQLSTATE [42000]:语法错误或访问冲突:1064 SQL语法中有错误;查看与您的MySQL服务器版本对应的手册,以获得正确的语法,以便在' ORDER BY id asc LIMIT 0,10'附近使用。在第2行(SQL:SELECT tb_list。 FROM tb_list WHERE username = ORDER BY id asc LIMIT 0,10)*
提前致谢
假设会话所有者的用户名是john
Table with NO session
WHERE tb_list.task_id IS NOT NULL
+---------+----------+-------------+-----------------+
| task_id | username | some_data | some_time |
+---------+----------+-------------+-----------------+
| 1 | john | some data | 2015-02-29 1 pm |
| 2 | jack | some data | 2015-02-29 2 pm |
| 3 | john | some data | 2015-02-29 3 pm |
| 4 | bill | some data | 2015-02-29 4 pm |
+---------+----------+-------------+-----------------+
Table with john's session
WHERE username=".Session::get('username')."
+---------+----------+-------------+-----------------+
| task_id | username | some_data | some_time |
+---------+----------+-------------+-----------------+
| 1 | john | some data | 2015-02-29 1 pm |
| 3 | john | some data | 2015-02-29 3 pm |
+---------+----------+-------------+-----------------+
答案 0 :(得分:0)
我认为这是因为你缺少单引号。当你将字符串值传递给sql查询语句时,你必须放入单引号。 例如,
SELECT tb_list.* FROM tb_list
WHERE myusername = '".Session::get('username')."';
试试这个。
答案 1 :(得分:0)
return " WHERE username = '".Auth::user()->username."' ";
这就成功了。