douzhuan1467
2016-11-26 06:58
浏览 58
已采纳

Laravel - 如何在查询中使用会话用户名

I am trying to display a table based on session username but I am getting the following error. When it is id it is working but when it is username it is not

SELECT tb_list.* FROM tb_list  
WHERE myusername = ".Session::get('username')."

QueryException in Connection.php line 662: SQLSTATE[42000]: Syntax error or access violation: 1064 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 'ORDER BY id asc LIMIT 0 , 10' at line 2 (SQL: SELECT tb_list. FROM tb_list WHERE username = ORDER BY id asc LIMIT 0 , 10 )*

Thanks in advance

Lets say the session owner's username is 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 |
+---------+----------+-------------+-----------------+

图片转代码服务由CSDN问答提供 功能建议

我正在尝试显示基于会话用户名的表,但是我收到以下错误。 当它是 id 时它正在工作但是当它是 username 时它不是

  SELECT tb_list。* FROM tb_list \  nWHERE myusername =“。Session @ get('username')。”
   
 
 

Connection.php第662行中的QueryException:SQLSTATE [42000]:语法 错误或访问冲突:1064您的SQL语法有错误; 检查与MySQL服务器版本对应的手册,以便在第2行的“ORDER BY id asc LIMIT 0,10”附近使用正确的语法(SQL:SELECT tb_list。 FROM tb_list WHERE username = ORDER BY id asc LIMIT 0,10)*

提前致谢

让我们说会话所有者的用户名是john

 <  code>没有会话的表
WHERE tb_list.task_id IS NOT NULL 
 + --------- + ---------- + -------------  + ----------------- + \ N |  task_id | 用户名|  some_data |  some_time | 
 + --------- + ---------- + ------------- + -----------  ------ + \ N |  1 | 约翰| 一些数据|  2015-02-29 1 pm | 
 |  2 | 杰克| 一些数据|  2015-02-29 2 pm | 
 |  3 | 约翰| 一些数据|  2015-02-29 3 pm | 
 |  4 | 账单| 一些数据|  2015-02-29 4 pm | 
 + --------- + ---------- + ------------- + -----  ------------ + 
 
表示约翰的会话
WHERE username =“。Session :: get('username')。”
 + --------- +  -  --------- ------------- + + ----------------- + \ N |  task_id | 用户名|  some_data |  some_time | 
 + --------- + ---------- + ------------- + -----------  ------ + \ N |  1 | 约翰| 一些数据|  2015-02-29 1 pm | 
 |  3 | 约翰| 一些数据|  2015-02-29 3 pm | 
 + --------- + ---------- + ------------- + -----  ------------ + 
   
 
  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • douzhang8033 2016-11-26 10:12
    已采纳

    I think it is because you are missing single quotes.When you pass a string value into an sql query statement you have to put single quotes. For example,

    SELECT tb_list.* FROM tb_list WHERE myusername = '".Session::get('username')."';

    Try this one.

    评论
    解决 无用
    打赏 举报
查看更多回答(1条)

相关推荐 更多相似问题