简体   繁体   English

想要在我的PHP SQL查询中使用SESSION变量

[英]Want to use a SESSION variable in my PHP SQL Query

This has been driving me mad for weeks, I just want a $_SESSION variable included in a query. 这已经让我发疯了数周,我只想在查询中包含$ _SESSION变量。

It's worked on every other page but this one, this page and this specific one just WILL NOT WORK. 它在其他所有页面上都起作用,但是此页面,该页面以及该特定页面将无法使用。

$query = "
    SELECT
        *
    FROM listings
    WHERE
    id = :id
    ";
    // Define our query parameter values
        $query_params = array(
            ':id' => htmlentities($_SESSION['user']['id'], ENT_QUOTES, 'UTF-8')
            );

Is what I have essentially wound up with, and as far as I know it should work like it has on every other page! 这是我真正想到的,就我所知,它应该像其他页面一样工作!

I've tried concatonating and echo of it, I've tried just session, I've tried POST, but it just won't seem to let it in. 我已经尝试过使之回声,我尝试过仅会话,已经尝试过POST,但似乎并没有让它进入。

Earlier in the page I have 在页面的前面,我有

<?php echo htmlentities($_SESSION['user']['id'], ENT_QUOTES, 'UTF-8') ?>

As a test to make sure the value is there, and it works flawlessly. 作为确保该值存在的测试,它可以完美工作。

I've also tried listing the "SELECTS" specifically and still nothing. 我也尝试过专门列出“ SELECTS”,仍然一无所获。

Here is the error PHP gives me: "Invalid parameter number: no parameters were bound" 这是PHP给我的错误:“无效的参数编号:未绑定任何参数”

try this 尝试这个

$paramSession = htmlentities($_SESSION['user']['id'], ENT_QUOTES, 'UTF-8');
$query_params = array(YOUR_PARAMS)
print_r($query_params); //edit his post adding the output of this.

Try this if it works, assuming you have $db as your DB connection object 假设您有$ db作为数据库连接对象,请尝试使用此方法

$query = "
SELECT
    *
FROM listings
WHERE
id = :id
";
$stmt = $db->prepare($query);
$stmt->bindValue(':id', htmlentities($_SESSION['user']['id'], ENT_QUOTES, 'UTF-8'));

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

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