[英]Selecting Data from SQL Database Returns “1”
This is my first SQL Database. 这是我的第一个SQL数据库。 I've been able to successfully connect to my server and database. 我已经能够成功连接到我的服务器和数据库。 But, when I use a query to select data from one of the columns in my table, it returns the number "1". 但是,当我使用查询从表中的一列中选择数据时,它返回数字“1”。 Why is this? 为什么是这样?
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "dbname";
$conn = mysqli_connect($servername, $username, $password, $dbname);
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$sql = "SELECT Item FROM Items";
$result = $conn->query($sql);
echo "<h1>Connected successfully<h1>";
echo "<p class='lead'>" + $result + "</p>";
$conn->close();
?>
Here's an image of the table named Items
这是名为Items
的表的图像
This is what shows up on the webpage where it should echo
the contents in the item
column: 这是在网页上显示的内容,它应该echo
显item
列中的内容:
You're just echoing the whole object, not individual rows. 你只是回显整个对象,而不是单个行。 You need to do something like this to iterate over each row. 你需要做这样的事情来迭代每一行。
if ($result = $conn->query($query)) {
/* fetch object array */
while ($row = $result->fetch_row()) {
echo "<p class='lead'>" . $row[0] . "</p>";
}
/* free result set */
$result->close();
}
This is because 2 different things: 这是因为有两件事:
+
simbol not with the .
你试图用+
simbol连接一个字符串而不是.
simbol. 辛博尔。 $result
variable contains a mysql_result
, because the query was executed. 您的$result
变量包含mysql_result
,因为查询已执行。 If you want to echo your data you must to use $result
into a while loop
. 如果要回显数据,则必须将$result
用于while loop
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.