0

I have a table product that has columns of id, name, price, desc. But I only need to display price of a searched product.

For example, when user enters 0001, then the query should execute and return a value to $price of $5.00.

$sqlprice = $DB->Prepare("SELECT price FROM product WHERE id = '0001'");
$sqlprice -> Execute();
$result = $sqlprice->get_result();
$price = $result->fetch_assoc();

However, the $price variable does not has any value in it. What I did wrong in my code? I've searched across many threads but it don't seems to have an answer for my problem.

Cœur
  • 37,241
  • 25
  • 195
  • 267
hatched
  • 795
  • 2
  • 9
  • 34
  • 2
    do you have an entry with id = `0001` (keeping in mind that `'0001'` is not the same as `'1'`) ? – apokryfos May 30 '18 at 09:16
  • `->fetch_assoc` won't return the field, it returns an array .. have you `var_dump`'d the `$price` var? – treyBake May 30 '18 at 09:16
  • @apokryfos yes, its 0001 – hatched May 30 '18 at 09:16
  • @ThisGuyHasTwoThumbs I do not need an array, just a single value, no i didn't – hatched May 30 '18 at 09:17
  • Then if all goes well `$price` should be an associative array like `[ 'price' => 5 ]` or something like that. That's how that works. If there's nothing in there then that means that that row just does not exist. There's no "fetch single value" method in PDO or MySQLi you have to roll your own – apokryfos May 30 '18 at 09:19
  • What is the `$DB`? PDO? MysqlI? – Pyton May 30 '18 at 09:28
  • @hatched you should var_dump it - you will see a value under the key of price .... it won't ever just return the value without a custom method implemented. It will either return an array so `$data['price']` or an object, so `$data->price` – treyBake May 30 '18 at 09:28

7 Answers7

0

assuming that $sqlprice is an instance of mysqli i recommend using $sqlprice->fetchAll(PDO::FETCH_ASSOC)

Max
  • 180
  • 4
  • 13
0

Access your price with $price[0] or $price['price']. If $price should be empty, then your SQL query was not successful. You should handle this case accordingly.

I advise you to get familiar with the var_dump() and print_r() methods for debugging purposes :)

Adrian Baginski
  • 336
  • 1
  • 8
0

$result->fetch_assoc() returns an associative array, which you can acces like $array["name"].

If the query is correct, you should get the price by accessing $price["price"]. But if $price does not has any value in it (=== null), then there is something wrong with the execution of your query / there is just no value in the DB.

You could also try the following (assuming your using PDO) and see if that works:

$sql = 'SELECT price FROM product WHERE id = "0001"';
foreach ($conn->query($sql) as $row) {
    echo $row['price'];
}

For mysqli:

$sql = 'SELECT price FROM product WHERE id = "0001"';
$result = $conn->query($sql);
while($row = $result->fetch_assoc()) {
    echo $row['price'];
}
  • made my entire page blank – hatched May 31 '18 at 02:36
  • check your server logs or turn error logging on. "made my entire page blank" doesn't give me ANY information to help you out here. Are there errors being thrown? Is there just nothing in your database? Did it show something before? How did you implement it (i hope you didn't just copy paste)? Show us something –  May 31 '18 at 06:28
  • its an existing page for years , just need to add a new query and my query doesn't return value I need, however by using your method, my entire page just went blank, but I had solved my problem already, thanks for your help – hatched May 31 '18 at 06:42
  • Glad to hear that it's solved! However, you're saying that "your entire page went blank", don't you have error logging turned on? Cause it shouldn't just show a blank page. If it does, there should be an error log written somewhere. What does that say? –  May 31 '18 at 06:59
0

I don't really know why ...but this works for me after many tries.

$sqlprice= "SELECT price FROM product WHERE id='0001'";
        $rs = $DB -> Execute($sqlprice);
        while ($row = $rs -> FetchRow()) {
          $price= $row["price"];
        }
hatched
  • 795
  • 2
  • 9
  • 34
-1

You can check step by step where is the problem

if ($sqlprice = $DB->Prepare("SELECT price FROM product WHERE id = '0001'")) {
    if($sqlprice->execute()) {
        $result = $sqlprice->get_result();
        $price = $result->fetch_assoc();
    } else {
        die($sqlprice->error)
    }
} else {
    die($DB->error);
}
treyBake
  • 6,440
  • 6
  • 26
  • 57
Mithu CN
  • 605
  • 5
  • 11
  • *the $price variable does not has any value in it*. Seems like this will just throw an undefined index error. – Loek May 30 '18 at 09:18
-1

I assume that 0001 is ID of the product in your product table. If this is so then you can try this query:

$this->db->select('price');
$this->db->where('id','id_entered_by_user');
$query = $this->db->get('product');

Your result will be stored in $query and you can check it by printing print_r($query->result());

Let me know that this query worked for you or not.

-1
$conn = @new mysqli($host, $db_user, $db_password, $db_name);
$sql = "SELECT price FROM product WHERE id = '0001'";
$result = mysqli_query($conn, $sql);
$price = mysqli_fetch_assoc($result)["price"];
Ser
  • 37
  • 6