dtah63820
dtah63820
2011-04-16 01:08

SQL查询中的变量不起作用?

已采纳
    if (isset($_SESSION['user_tz'])) {
        $posted = "CONVERT_TZ(p.posted_on, 'UTC', '{$_SESSION['user_tz']}')";
    } else {
        $posted = 'p.posted_on';
    }

    // Run the query:
    $q = "SELECT t.subject, p.message, username, DATE_FORMAT($posted, '%e-%b-%y %l:%i %p') AS posted FROM threads AS t LEFT JOIN posts AS p USING (thread_id) INNER JOIN users AS u ON p.user_id = u.user_id WHERE t.thread_id = $tid ORDER BY p.posted_on ASC";

I changed the $posted in the query to a plain "posted_on" which returned the time, I also tried some wrapping it in '' and "" but those ended up breaking it entirely; for future reference I'd like to know why that variable isn't getting passed through to the query. It's probably something really simple and I'll feel silly but help would be appreciated greatly.

Thanks.

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

2条回答

  • dongzhoulong1797 dongzhoulong1797 10年前

    NULL is a valid value for isset() to trigger TRUE. Use unset($_SESSION['user_tz']);

    点赞 评论 复制链接分享
  • dongshiqin1352 dongshiqin1352 10年前

    It seems to me that the way you have it written, it is using $posted as the value to pass to the date_format. What you really want is the contents of $posted so you need to close quotes around it and concatenate the value into the $q string.

    点赞 评论 复制链接分享