PHP SELECT*FROM无法正常工作

jpg*_*erb 0 php mysqli

我试图使用以下脚本从表中查询数据:

//connect file
$conn = new mysqli($servername, $username, $password, $dbname);

// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

//connect file included above - ECHO tested and it is connected as $conn

$sql = "SELECT * FROM userInfo";
$results = $conn->query($sql);
if (!$results) {
    printf("Errormessage: %s\n", $conn->error);
    exit;
} else {
    echo $row['username'];
}
Run Code Online (Sandbox Code Playgroud)

更新 -

它现在不再试图抛出错误,似乎转到该else部分; 但是,没有回声 - 这次拼写是正确的并且列被填充.

jpg*_*erb 5

if ($result->num_rows > 0) {
    // output data of each row
    while($row = $result->fetch_assoc()) {
        echo $row["username"];
    }
} else {
    echo "0 results";
}
Run Code Online (Sandbox Code Playgroud)

现在返回结果.谢谢@Fred -ii-特别感谢您的帮助.

还要感谢@jjczopek的错误检查建议!