【问题标题】:How use session variable in sql using php如何使用 php 在 sql 中使用会话变量
【发布时间】:2017-12-09 20:49:43
【问题描述】:

我在显示登录用户添加的文件时遇到问题。 我不知道如何将变量正确传递给 sql 查询。 谁能帮我解决这个问题?

目前,代码如下所示:

    <?php
include_once 'dbconnect.php';
?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>File Uploading With PHP and MySql</title>
<link rel="stylesheet" href="style.css" type="text/css" />
</head>
<body>
<div id="header">
<label>File Uploading With PHP and MySql</label>
</div>
<div id="body">
 <table width="80%" border="1">
    <tr>
    <th colspan="4">your uploads...<label><a href="index.php">upload new files...</a></label></th>
    </tr>
    <tr>
    <td>File Name</td>
    <td>File Type</td>
    <td>File Size(KB)</td>
    <td>View</td>
    </tr>
    <?php
 $sql="SELECT * FROM files";
 $result_set=mysql_query($sql);
 while($row=mysql_fetch_array($result_set))
 {
  ?>
        <tr>
        <td><?php echo $row['file'] ?></td>
        <td><?php echo $row['type'] ?></td>
        <td><?php echo $row['size'] ?></td>
        <td><a href="uploads/<?php echo $row['file'] ?>" target="_blank">view file</a></td>
        </tr>
        <?php
 }
 ?>
    </table>

</div>
</body>
</html>

我正在尝试更改此记录:

$sql="SELECT * FROM files";

$sql="SELECT file, type, size FROM files WHERE userId ='$_SESSION[userId]'";

但我仍然没有得到正确的结果。有人可以帮忙吗?

【问题讨论】:

标签: php sql session variables


【解决方案1】:

看起来该行的问题在于您如何包含 $_SESSION 变量。您应该在userId 周围加上引号,例如$_SESSION['userId']{$_SESSION['userId']}

更重要的是,您应该避免将变量直接输入到 MySQL 查询中。我建议使用MySQLiPDO 而不是MySQL,并查看准备好的语句(例如herehere)。

【讨论】:

    猜你喜欢
    • 2017-08-29
    • 2016-02-29
    • 2015-07-28
    • 2011-03-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多