我正在尝试从mysql数据库中选择一个SINGLE值。我在phpmyadmin中运行了查询,效果很好。但是当我回应$ result时,我什么都没得到......顺便说一下,对于我使用xxx的数据库和密码,因为我不想显示它...我的插入查询工作得很好
由于
<?php
//Create Connection
$servername = "localhost";
$username = "root";
$password = "xxx";
$dbname = "xxx";
//Connect
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT StartPriceUnder FROM YJ_Value";
$result = $conn->query($sql);
echo hi;
echo $result;
echo ya;
$conn->close();
?>
答案 0 :(得分:1)
试试这个:
<?php
$servername = "localhost";
$username = "root";
$password = "xxx";
$dbname = "xxx";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT StartPriceUnder FROM YJ_Value";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "StartPriceUnder:" . $row["StartPriceUnder"];
}
}
else {
echo "0 results";
}
$conn->close();
?>
答案 1 :(得分:0)
您必须获取结果,所以请执行以下操作:
$row = $result->fetch_array(MYSQLI_ASSOC);
在此之后你可以像这样回应它:
echo $row["StartPriceUnder"];
有关fetch_array()
的详情,请参阅手册:http://php.net/manual/en/mysqli-result.fetch-array.php