douhuang5331 2011-04-21 15:55
浏览 62
已采纳

mysql查询中的$ _SESSION变量?

Whenever I try a query like:

mysql_query("SELECT * FROM data WHERE `user`=$_SESSION['valid_user'] LIMIT 1");

it doesn't work. Why? I escaped the variable, then tried it without, and tried putting quotes around the variable. I know i can do:

$user = $_SESSION['valid_user'];

but shouldn't it work without? Thanks.

THE ANSWER:

PHP can't recognize $_SESSION['valid_user'] due to the single quotes. So either use curly braces {} or take our the single quotes.

Thanks for helping me everyone.

  • 写回答

6条回答 默认 最新

  • douce1368 2011-04-21 15:58
    关注

    PHP can't recognise variables inside a string that have square brackets and so on, you have to wrap it in curly brackets to get it to recognise it.

    mysql_query("SELECT * FROM data WHERE user={$_SESSION['valid_user']} LIMIT 1");
    

    However - You should always escape any data going into a SQL query, try the example below.

    $validUser = mysql_real_escape_string($_SESSION['valid_user']);
    mysql_query("SELECT * FROM data WHERE user='$validUser' LIMIT 1");
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(5条)

报告相同问题?

悬赏问题

  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化