duanniu3385 2016-05-02 08:48
浏览 16
已采纳

用相同的用户名php显示结果

I have a database which stores data. How can I view data in my database with the same username as my session? What I have tried is below. There is a session and the username is uploading in each row in the database.

This is what I'm trying to do: say I logged in as jack I typed data in and sent it to the database. It saves the name as jack and then only views the results with jack. But it is saying 0 results. Why?

<?php
session_start();
if (isset($_SESSION['username'])) {
    $username = $_SESSION['username'];
    echo "$username";
}

$servername = "localhost";
$username = "root";
$password = "root";
$dbname = "score";

$conn = new mysqli($servername, $username, $password, $dbname);

if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 

$sql = "SELECT id, name, description FROM all_scores WHERE username = '".$username."' ORDER BY id DESC LIMIT 5";
$result = $conn->query($sql);

if ($result->num_rows > 0) {    
    while($row = $result->fetch_assoc()) {
        echo "<p></p>";
        echo "<a href=score_2/view.php?pageid=" . $row["id"] . ">". $row["name"]. "</a>";
        echo "<p>". $row["description"]. "</p>";
    }
} else {
    echo "0 results";
}

$conn->close();

?>
  • 写回答

1条回答 默认 最新

  • duan0065626385 2016-05-02 08:57
    关注

    you have two mistakes

    1- SQL syntax error, correct syntax is

    $sql = "SELECT id, name, description FROM all_scores WHERE username = '".$username."'";

    2- the variable $username is overwritten by the username of the database try this:

    $sql = "SELECT id, name, description FROM all_scores WHERE username = '".$_SESSION['username']."'";
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器