在非对象上调用fetch_assoc()是否失败? [英] Call to fetch_assoc() on a non-object failing?

查看:124
本文介绍了在非对象上调用fetch_assoc()是否失败?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在phpMyAdmin中创建了一个名为"whatevers"的表.您可以看到它的内容

I have created a table in my phpMyAdmin called 'whatevers'. You can see the contents of it

.

我正在尝试从该表中获取所有条目(记录?)并回显它们.

I'm trying to fetch all the entries (records?) out of this table and echo them.

这是我尝试使用的代码:

This is the code I have attempted to do this with:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
    <head>
        <title>Welcome to <? print $host; ?>!</title>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

    </head>
    <body>

<?php
$servername = "mysql.hostinger.co.uk";
$username = "u427788796_h4x1t";
$password = "********";
$dbname = "u427788796_db";

// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
}

////


$sql = "SELECT * FROM `whatevers` LIMIT 0, 30 ";

//echo $sql; 

while ($row = $sql->fetch_assoc()) {
        echo $row['classtype']."<br>";
    }





mysqli_close($conn);
?>

    </body>
</html>

这是失败的,因为我收到了错误消息:

This is failing as I am getting the error message:

致命错误:在第30行的/home/u427788796/public_html/default.php中的非对象上调用成员函数fetch_assoc()

推荐答案

已调用方法查询

$sql = $conn->query("SELECT * FROM `whatevers` LIMIT 0, 30 ");

这篇关于在非对象上调用fetch_assoc()是否失败?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆