Search code examples
phpmysqlphpmyadminzend-studio

php unable to select data


I'm trying to select a SINGLE value from the mysql database. I have run the query in phpmyadmin and it work great. But when I echo the $result, I get nothing... by the way,for the database and password I use xxx because I don't want to show it... My insert query works very well

Thanks

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

?>

Solution

  • Try this:

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