用相同的用户名php

时间:2016-05-02 08:48:36

标签: php

我有一个存储数据的数据库。如何使用与会话相同的用户名查看数据库中的数据?我试过的是下面的内容。有一个会话,用户名正在数据库的每一行上传。

这就是我尝试做的事情:说我以jack的形式登录我输入的数据并将其发送到数据库。它将名称保存为插孔,然后仅使用插孔查看结果。但它的结果是0。为什么呢?

<?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 个答案:

答案 0 :(得分:3)

你有两个错误

1- SQL语法错误,正确的语法是

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

2-变量$username被数据库的用户名覆盖 试试这个:

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