-3

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

?>
Rain
  • 101
  • 1
  • 1
  • 8

2 Answers2

1

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();
?> 
d_unknown
  • 875
  • 3
  • 15
  • 41
  • 1
    For reference: http://www.w3schools.com – d_unknown Mar 13 '15 at 02:35
  • 1
    Now you have a reference, code, but no explanation what you changed and why you changed it and what was wrong with OP's code – Rizier123 Mar 13 '15 at 02:39
  • 1
    it's because he didn't fetch the result.. – d_unknown Mar 13 '15 at 02:43
  • I just added the if-else statement and the while loop to output each row.. And he can't just echo the `$result` like that.. – d_unknown Mar 13 '15 at 03:14
  • Thank you very much, your code work very well! – Rain Mar 13 '15 at 12:38
  • one more question. When I do echo $row["StartPriceUnder"]; or $result=$row["StartPriceUnder"], it doesn't work – Rain Mar 13 '15 at 12:44
  • I think I figure out, I have to put the $result command within the while statement – Rain Mar 13 '15 at 12:46
  • but when i put the echo $result outside of while statement it won't echo 0 0 – Rain Mar 13 '15 at 12:47
  • you mean, you will assign the `$row["StartPriceUnder"]` in a `$result` variable? yes, you have to put it inside the while loop. But if the result of the query is more than one then you have to put it also in an array and echo it using while loop. – d_unknown Mar 13 '15 at 13:10
  • maybe you can use other variable because `$result` is already used and for you not to be confused of the variables. – d_unknown Mar 13 '15 at 13:24
0

You have to fetch your result, so do something like this:

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

After this you can echo it like this:

echo $row["StartPriceUnder"];

For more information about fetch_array() see the manual: http://php.net/manual/en/mysqli-result.fetch-array.php

Rizier123
  • 58,877
  • 16
  • 101
  • 156