簡體   English   中英

如何使用PHP在SQL中使用會話變量

[英]How use session variable in sql using php

我在顯示由登錄用戶添加的文件時遇到問題。 我不知道如何將變量正確傳遞給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]'";

但是我仍然沒有得到正確的結果。 有人可以幫忙嗎?

該行的問題似乎在於如何包含$ _SESSION變量。 您應該在userId周圍userId引號,例如$_SESSION['userId']{$_SESSION['userId']}

更重要的是,您應該避免直接在MySQL查詢中輸入變量。 我建議使用MySQLiPDO代替MySQL,並查看准備好的語句(例如, 此處此處 )。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM