简体   繁体   English

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

[英]Variable in SQL Query Not Working?

    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; 我将查询中的$ post更改为一个简单的“ posted_on”,它返回了时间,我也尝试将其包装在''和“”中,但最终将它们完全破坏了。 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. 谢谢。

NULL is a valid value for isset() to trigger TRUE. NULL是isset()触发TRUE的有效值。 Use unset($_SESSION['user_tz']); 使用unset($_SESSION['user_tz']);

It seems to me that the way you have it written, it is using $posted as the value to pass to the date_format. 在我看来,您编写它的方式是将$ posted用作传递给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. 您真正想要的是$ posted的内容,因此您需要在其两边加上引号并将其值连接到$ q字符串中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM