如何从sql数据库中获取值并从php会话中访问它们

时间:2018-01-29 17:50:24

标签: php sql session

我现在已经尝试了2天来访问sql数据库并获取" username"的值。和"性别" (成功),并将它们放入会话(不成功)。我不知道如何修改代码以使其工作,尽管通过这个网站查看,查看示例,徒劳地搜索异地。 我对编码很新,任何帮助都将不胜感激。感谢信。

<?php
session_start();

$servername = "11111111";
$username = "username";
$password = "password";
$dbname = "dbtest";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 

$sql = "SELECT id_user, username, gender FROM fgusers3";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
    // output data of each row
    while($row = $result->fetch_assoc()) {
        echo "<br> id_user: ". $row["id_user"]. " - username: ". $row["username"]. " - gender: " . $row["gender"] . "<br>";
    }
} else {
    echo "0 results";
}

$conn->close();

$_SESSION[$key]=$value;
$_SESSION['username']=$row['username'];
$_SESSION['gender']=$row['gender'];
echo $_SESSION["username"] . ".<br>";
echo $_SESSION["gender"] . ".";
?>

2 个答案:

答案 0 :(得分:0)

当你获得多行时,数据必须以某种数组的形式存储在SESSION中,所以这里有一个建议可以创建像这样的视图。

$_SESSION['usernames'][0] = 'Fred'
$_SESSION['usernames'][1] = 'Frederica'

$_SESSION['genders'][0] = 'Male'
$_SESSION['genders'][1] = 'Female'

代码mods

<?php
session_start();

$servername = "11111111";
$username = "username";
$password = "password";
$dbname = "dbtest";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 

$sql = "SELECT id_user, username, gender FROM fgusers3";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
    // output data of each row
    while($row = $result->fetch_assoc()) {
        echo "<br> id_user: ". $row["id_user"]. " - username: ". $row["username"]. " - gender: " . $row["gender"] . "<br>";

        // Add to the session inside the loop

        $_SESSION['usernames'][] = $row['username'];
        $_SESSION['genders'][] = $row['gender'];
    }
} else {
    echo "0 results";
}

可能是一个更清洁的解决方案

if ($result->num_rows > 0) {
    // output data of each row
    while($row = $result->fetch_assoc()) {
        echo "<br> id_user: ". $row["id_user"]. " - username: ". $row["username"]. " - gender: " . $row["gender"] . "<br>";

        // Add to the session inside the loop

        $_SESSION['users'][] = ['username' =>$row['username'],
                                'gender'] => $row['gender']
                               ];
    }
} else {
    echo "0 results";
}

答案 1 :(得分:0)

以下是正在运行的代码:

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 

$sql = "SELECT id_user, username, gender FROM fgusers3";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
    // output data of each row
    while($row = $result->fetch_assoc()) {
        "<br> id_user: ". $row["id_user"]. " - username: ". $row["username"]. " - gender: " . $row["gender"] . "<br>";


       $_SESSION['username'] = $row['username'];
       $_SESSION['gender'] = $row['gender'];
    }
} else {
    echo "0 results";
}
$conn->close();