php - 在非对象上调用成员函数fetch_assoc()

时间:2015-10-08 11:50:49

标签: php mysql

表格结构:

user table:
id:int
user:varchar
hash:varchar
realm:int

Game Table:
ID:int:auto
Game:varchar
Password:varchar
Difficulty:tinytext
Realm:tinytext
Empty:int
timestamp:int

Player Table:
ID:Int
GameID:INT
Name:varchar
timestamp:int

当我尝试fetch_assoc()时,我收到错误:

在非对象

上调用成员函数fetch_assoc()
function RequestToRemove($Hash, $timestamp)
    {
        include "conf.php";
        $conn = new mysqli($serverip, $username, $password, $dbname, $Port);
        if ($conn->connect_error) {
            die("Connection failed: " . $conn->connect_error);
        } 
        $hash = mysqli_escape_string($conn, $Hash);
        $UserQuerry = "SELECT * from User where hash = '$hash'";
        $UserResult = $conn->query($UserQuerry);
        if (!$UserResult) {
             echo "hash not found: ".$hash;
        }
        if ($UserResult->num_rows > 0) 
        {

            while($UserRow = $resulti->fetch_assoc()) 
            {
               $PlayerQuerry = "SELECT * from Players where ID = '".$UserRow['id']."'";
               $PlayerResult = $conn->query($PlayerQuerry);
               while($PlayerRow = $PlayerResult->fetch_assoc()) 
                {
                    $gameID = $PlayerRow['GameID'];
                    $gameQuerry = "SELECT * from games where ID = '$gameID'";
                    $gameResult = $conn->query($gameQuerry);
                    if ($gameResult->num_rows > 0) 
                    {
                        while($GameRow = $gameResult->fetch_assoc()) 
                        {
                            if((int)$GameRow['timestamp'] <= ($timestamp - 30) && (int)$GameRow['Empty'] == 1)
                            {
                                $gameDeleteQuerry = "DELETE FROM games where id = '".$gameID."'";
                                $conn->query($gameDeleteQuerry);
                                echo "Game Updated";
                            }
                        }
                        $playerDeleteQuerry = "DELETE FROM Players where id = '$gameID'";
                        $gamesUpdateQuerry = "UPDATE games SET timestamp='$timestamp' where ID = '$gameID'";
                        $gamesUpdateResult = $conn->query($gamesUpdateQuerry);
                        if (!$gamesUpdateResult) {
                            echo "Can't UPDATE game! ID: $gameID Error: ". $conn->error;
                            return;
                        }
                        $conn->query($playerDeleteQuerry);
                        $PlayerQuerry = "SELECT * from players where GameID = '$gameID'";
                        $PlayerResult = $conn->query($PlayerQuerry);
                        if (!$PlayerResult) {
                            echo "Can't get players! Error: " . $conn->Error;
                            return;
                        }
                        if ($PlayerResult->num_rows <= 0) {
                            $gameUpdateEmptyQuerry = "UPDATE games SET Empty='1' where GameID = '".$gameID."'";
                            $conn->query($gameUpdateEmptyQuerry);
                        }

                    } else {
                        echo "0 results";
                    }
                }
            }
        }
        $conn->close();
    }

但是当我正在运行

SELECT * from User where hash = 'ae3cb232b5e489050bfed7ed984eb04c'
<\ n>在HeidiSQL中 我会得到一行作为回应,但我仍然不能在php

中使用fetch_assoc()

编辑原因:显示完整功能,请求

1 个答案:

答案 0 :(得分:0)

您正在循环中为$resulti重新分配不同的值:

$sqll = "DELETE FROM Players where id = '$gameID'";
$sqlu = "UPDATE games SET timestamp='$timestamp' where ID = '$gameID'";
$resulti = $conn->query($sqlu);

mysqli :: query()函数很棘手,它可以返回不同类型的返回值 - update语句返回true或false,而select语句返回mysqli_result或false。

(是的,将变量命名为更具描述性可能会有所帮助;))