[英]PHP/MYSQLI Error: Notice: Trying to get property of non-object in
I am trying to echo an entire table from a database, but everytime I try to fix the 'Notice: Trying to get property of non-object in /home/st359450/public_html/epw/database.php on line 24' error, it won't work. 我试图从数据库中回显整个表,但是每次尝试修复“通知:试图在第24行的/home/st359450/public_html/epw/database.php中获取非对象的属性”错误时,它将无法正常工作。 In my browser I have confirmation that the connection to the database is working.
在我的浏览器中,我确认到数据库的连接正在运行。
The PHP code after establishing the connection is: 建立连接后的PHP代码为:
<?php
$servername = "localhost";
$username = "359450";
$password = "dJDgxR";
//create connection
$conn = mysqli_connect($servername, $username, $password);
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
echo "Connected successfully <br> <br>";
$sql = "SELECT klantnummer, voornaam, tussenvoegsel, achternaam, straat, huisnummer, postcode, woonplaats, telefoonnummer, emailadres FROM CMI_klanten";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "Klantnummer: " . $row["klantnummer"] . "Naam: " . $row["voornaam"]. " " . $row["tussenvoegsel"]. " " . $row["achternaam"] . "Adres: " . $row["straat"] . " " . $row["huisnummer"] . " " . $row["woonplaats"] . "Telefoonnummer: " . $row["telefoonnummer"] . "Emailadres: " . $row["emailadres"] . "<br>";
}
} else {
echo "0 results";
}
$conn->close();
?>
Everything in the $sql variable is spelled correctly. $ sql变量中的所有内容均拼写正确。 I have checked it multiple times.
我已经检查了多次。 Even though the error is displayed, it still outputs '0 results'.
即使显示错误,它仍然输出“ 0结果”。 I have checked multiple similar questions, but none of them were able to fix my problem.
我已经检查了多个类似的问题,但是没有一个能够解决我的问题。 The error is in the following bit:
错误在以下位:
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "Klantnummer: " . $row["klantnummer"] . "Naam: " . $row["voornaam"]. " " . $row["tussenvoegsel"]. " " . $row["achternaam"] . "Adres: " . $row["straat"] . " " . $row["huisnummer"] . " " . $row["woonplaats"] . "Telefoonnummer: " . $row["telefoonnummer"] . "Emailadres: " . $row["emailadres"] . "<br>";
}
} else {
echo "0 results";
}
$conn->close();
?>
I hope you guys can help me! 我希望你们能帮助我!
You are calling object methods from a procedural mysqli. 您正在从过程mysqli调用对象方法。 While I dont think this will break anything, for your own sanity and anyone who might see this code after you, I would pick one style and go with it consistently.
虽然我认为这不会破坏任何东西,但出于您自己的理智和可能在您之后看到此代码的任何人,我会选择一种样式并始终如一地使用它。
You cannot call num_rows immediately. 您不能立即调用num_rows。 Per the manual :
根据手册 :
The use of mysqli_stmt_num_rows() depends on whether or not you used mysqli_stmt_store_result() to buffer the entire result set in the statement handle.
mysqli_stmt_num_rows()的使用取决于您是否使用mysqli_stmt_store_result()在语句句柄中缓冲整个结果集。
Add this line after your query call and try again: 在查询呼叫后添加此行,然后重试:
$result->store_result();
You don't have a database parameter. 您没有数据库参数。 Enter your database value as the last parameter when initializing the mysqli connection:
初始化mysqli连接时,输入数据库值作为最后一个参数:
$database ='database_naam';
$conn = mysqli_connect($servername, $username, $password, $database);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.