PHP 无法选择数据


php unable to select data

我正在尝试从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();
?>

试试这个:

<?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();
?> 

你必须获取你的结果,所以做这样的事情:

$row = $result->fetch_array(MYSQLI_ASSOC);

在此之后,您可以像这样回显它:

echo $row["StartPriceUnder"];

有关fetch_array()的更多信息,请参阅手册:http://php.net/manual/en/mysqli-result.fetch-array.php