0
$query = $db->query("SELECT * FROM files WHERE hash = '" . $file . "'");
while($result = $query->fetch_array()) {
  $result[0] = $result['uploadr'];
  $result[1] = $result['name'];
}

这是查询和获取数组的代码,但它返回 null。有任何想法吗?顺便说一句,我尝试使用“$result[0]”和“$result['uploadr'];”来获得结果。有什么帮助吗?

4

1 回答 1

-2

不要重新分配您的阵列。只需像这样使用$result= query->fetch_assoc()和检索它们:

$upload = $result['uploadr'];
echo $upload;

为了使您的查询可靠地抵御攻击,您应该使用准备好的语句:

 $query = "SELECT * FROM files WHERE hash = ?"
if (!$stmt = $db->prepare($query))
    {
        echo "Prepared Failed: (" . $db->errno . ") " . $db->error;
    }
    if (!$stmt->bind_param("s", $file))
        {
            echo "Bind Failed: (" . $db->errno . ") " . $db->error;
        }
        if (!$stmt->execute())
            {
                echo "Execution Failed: (" . $db->errno . ") " . $db->error;
            }
            if (!$results = $stmt->get_results())
                {
                    echo "No Results where found: (" . $db->errno . ") " . $db->error;
                }
                while($row = $results->fetch_assoc())
                {
                    $upload = $row['uploadr'];
                    $name = $row['name'];
                    echo $upload."<br>";
                    echo $name;
                }
        $stmt -> close();
        $db -> close();
于 2013-01-03T00:54:12.100 回答