【问题标题】:$_SESSION variables in msqli_query [closed]msqli_query 中的 $_SESSION 变量
【发布时间】:2014-07-01 12:36:22
【问题描述】:

我正在尝试在 mqsli_query 语句中使用会话变量,但它不起作用

$result = mysqli_query($link, 'SELECT book_ID, Title, username as author from users,books
here users.user_ID = books.user_ID and username = "{$_SESSION['sess_username']}"' );

我尝试为 username = "" 使用真实值,它可以工作,因此这意味着查询很好。当我尝试使用会话变量时它不起作用

将用户名设置为实际值时的示例代码:

$result = mysqli_query($link, 'SELECT book_ID, Title, username as author from users,books
here users.user_ID = books.user_ID and username = "kllew"' );

【问题讨论】:

  • 语法高亮显示你的问题
  • 对不起,我不明白。 'sess_username' 为黑色的那部分?我对此真的很陌生。
  • 尝试使用prepared statements

标签: php variables session


【解决方案1】:

试试这个:(将会话放在 var 中,并更改您使用单引号的方式。

$session = $_SESSION['sess_username'];
$result = mysqli_query($link, "SELECT book_ID, Title, username as author from users,books
here users.user_ID = books.user_ID and username = '$session'" );

【讨论】:

  • 也一直在尝试。不起作用。无论如何感谢您的回答
  • 你的会话变量设置对了吗?
  • @alex:您忘记了 SQL 查询中值的引号。附:你可以这样做:"SELECT ... AND username = '{$_SESSION['sess_username']}'".
  • 哦,哎呀,甚至没有注意到,谢谢
  • @alex 很抱歉,但我对此真的很陌生,你把会话放在 var 中是什么意思? ..像这样? $_SESSION[$var] ?
【解决方案2】:

这里的“正确”方法是准备好的语句。您再也不需要将字符串连接到 SQL 查询中或担心引号。

$query = $link->prepare('SELECT book_ID, Title, username as author from users,books
    where users.user_ID = books.user_ID and username = ?');
$books = array();

if($query){
    // Bind the value to the `?`
    $query->bind_param('s', $_SESSION['sess_username']);
    $query->execute();
    // These variables will be created and populated with your values
    $query->bind_result($book_ID, $title, $author);
    while($query->fetch()){
        // Each time `fetch()` is, called, the variables will be
        // automagically updated with the next row's value
        // This while loop will run for each row, then stop
        $books[] = array(
            'book_ID' => $book_ID,
            'title' => $title,
            'author' => $author
        );
    }
}
else{
    die($link->error);
}

var_dump($books);

文档:http://www.php.net/manual/en/mysqli.prepare.php

编辑:如果您安装了mysqlnd 驱动程序(通常称为php-mysqlnd),那么您可以这样做:

$query = $link->prepare('SELECT book_ID, Title, username as author from users,books
    where users.user_ID = books.user_ID and username = ?');
$books = array();

if($query){
    // Bind the value to the `?`
    $query->bind_param('s', $_SESSION['sess_username']);
    $query->execute();

    // This allows you to use `fetch_array` like if you had used `mysqli_query`
    $result = $query->get_result();
    $books = $result->fetch_all(MYSQLI_ASSOC);
}
else{
    die($link->error);
}

var_dump($books);

【讨论】:

  • 我是否继续使用 $result = msqli_query($link, $query) ? ...
  • @user3632739:没有。$query->execute() 运行您的查询。当调用$query->fetch() 时,您的结果将存储在$book_ID$title$author 中。准备好的语句有点不同。
  • 那么我可以将 $book_ID、$title 和 $author 存储在一个数组中吗?如何?我需要将它们存储在一个数组中,因为我有将它们作为数组获取并显示它们的代码。
  • 5 月 1 日 -
  • 您的查询返回多少行?如果只有一个,那么你可以这样做:$result = array(); $query->bind_result($result['book_ID'], $result['title'], $result['author']); $query->fetch().
  • @user3632739:您可以简单地将它们添加到 while 循环中的数组中。
  • 猜你喜欢
    相关资源
    最近更新 更多
    热门标签