我一直在与这小段代码战斗大约2个小时,对其进行研究并试图找出我哪里出错了。经过9个小时的编码,我遇到了这个小问题,现在我陷入了困境。此时,我有3800行代码依赖于此,它已经死在水中。

问题在于最终变量都没有返回任何信息,它们完全返回为空白/空。当我尝试用“ $ getranks ['nickname']”分配“ myname”时,即使我的“ print_r($ getranks)”返回了所有这些信息,它甚至什么也没做。我也尝试使用fetch而不是fetchAll,但是后来我只得到一个结果。

我使用了一个旧的MYSQL网站,该网站是我在互联网上创建的一个小组的网站,该小组需要进行SQL注入,因此我完全使用PDO对其进行了重新制作。这是我的问题。

任何帮助深表感谢。我头疼,我要在等待协助时去吃晚饭。感谢大家。

代码:“ index.php”

$myid = $_SESSION['user'];

echo "User Session ID #" . $myid . ".<br><br>";

if(!empty($_SESSION['user'])) {
    $checkrank = $db->prepare("SELECT * FROM logins WHERE id = :myid");
    $checkrank->bindParam(':myid',$_SESSION['user']);
    $checkrank->execute();

    $getranks = $checkrank->fetchAll(PDO::FETCH_CLASS);

        print_r($getranks);
        $myname = $getranks['nickname'];
        $mycallsign = $getranks['callsign'];
        $mystatus = $getranks['status'];
        $mycallnum = $getranks['callnumber'];
        $myrank = $getranks['rank'];
        $mynote = $getranks['note'];
        $mybadge = $getranks['mybadge'];

    echo "<br><br>Check rank passed, the query came back with information.<br/>";
    echo "The session ID used to retrieve this information in the query was: " . $myid;
    echo "<br> Your name is " . $myname . ".";
}


结果:

    User Session ID #0102.

Array ( [0] => stdClass Object ( [id] => 0102 [badge] => 201 [rank] => 6 [nickname] => Rodger Anderson [username] => MASKED [password] => MASKED [salt] => MASKED [email] => MASKED [note] => [callsign] => [profile] => http://www.MASKED.net/forums/member.php?MASKED [callnumber] => [status] => ) )

Check rank passed, the query came back with information.
The session ID used to retrieve this information in the query was: 0102
Your name is .

You are SESSION[0102]
Your name is [] and your badge number is #.


旧代码:

$myid = $_SESSION['user'];

    echo "User Session ID #" . $myid . ".<br><br>";

    if(!empty($_SESSION['user'])) {
        $query = mysql_query("SELECT * FROM logins WHERE id = $myid");
        $getranks = mysql_fetch_assoc($query);

        $myname = $getranks['nickname'];
        $mycallsign = $getranks['callsign'];
        $mystatus = $getranks['status'];
        $mycallnum = $getranks['callnumber'];
        $myrank = $getranks['rank'];
        $mynote = $getranks['note'];
        $mybadge = $getranks['mybadge'];

        echo "<br><br>Check rank passed, the query came back with information.<br/>";
        echo "The session ID used to retrieve this information in the query was: " . $myid;
        echo "<br> Your name is " . $myname . ".";
    }

最佳答案

$getranks是对象数组,而不是关联数组。查看来自print_r($getranks)的结果。

你应该做类似的事情

$myname = $getranks[0]['nickname'];

或使用PDO::FETCH_ASSOC,但即使如此,您仍需要[0]。

09-18 02:22