Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have the following code:

$productQuery     = "SELECT ean, name FROM products WHERE category = '$category'";
$productResult    = $mysqli->query($productQuery);

while ($productRow = $productResult->fetch_assoc()) {
    $productPriceQuery     = "SELECT ean, price+shipmentCost AS totalPrice FROM prices WHERE ean = $productRow[ean] ORDER BY totalPrice ASC";
    $productPriceResult    = $mysqli->query($productPriceQuery);

    $totalPrice            = $productPriceResult->fetch_assoc();

    echo $productPriceResult["productURL"];
}

But I am getting the following error for the echo line:

PHP Fatal error:  Cannot use object of type mysqli_result as array in file.php

I already read some articles on this website and other websites but did not understand the problem and could not solve it myself.

share|improve this question
    
It seems that $productPriceResult is a sql result, not a fetched array. Is "productURL" stored in the products table? –  showdev Dec 5 '13 at 23:32
    
It is stored in table 'prices'. –  user2403721 Dec 5 '13 at 23:34
1  
When using mysqli you should be using parameterized queries and bind_param to add user data to your query. Avoid using string interpolation to accomplish this. –  tadman Dec 5 '13 at 23:39
add comment

2 Answers

$productPriceResult is a result, not a fetched array.

It seems like "productURL" should be stored in the products table, but for the sake of explanation I'll keep it how you have it: in the prices table.

You'll need to add "productURL" to your SELECT statement like this:

SELECT ean, productURL, price+shipmentCost AS totalPrice FROM prices ...

And access "productURL" like this:

$totalPrice['productURL'];

Incidentally, the price will be accessed like this:

$totalPrice['totalPrice'];

I recommend renaming the $totalPrice variable to $priceData or something more appropriate.

Edit:

Here's an example of how to loop through the price data.
I've truncated the actual query string for readability.

// loop through rows fetched from "products" table
while ($productRow = $productResult->fetch_assoc()) {

    // query price data for this product
    $productPriceQuery     = "SELECT ean, productURL, etc...";
    $productPriceResult    = $mysqli->query($productPriceQuery);

    // loop through rows fetched from "prices" table
    while ($priceData = $productPriceResult->fetch_assoc()) {
      echo "<p>".$priceData['productURL']."</p>";
    }

}
share|improve this answer
    
Stupid me :) I indeed have to use $totalPrice instead of $productPriceResult. Now it is partly working. I have 5 records in the table, 3x ean 123 and 2x ean 567. But is giving only 1 result. –  user2403721 Dec 6 '13 at 0:17
    
Nah, not stupid -- computers are just not very understanding ;). Which part is left not working? Or is that better as a separate post? –  showdev Dec 6 '13 at 0:20
    
Are you referring to the prices table? If so, you are only fetching from that table once per product. That only fetches the first returned row. To fetch more rows, you'll need a loop like the while loop you used for your first query. –  showdev Dec 6 '13 at 0:30
    
I added an example to my answer to illustrate the second loop. –  showdev Dec 6 '13 at 0:39
add comment

Try using

public function DisplayProducts() {
    $productQuery     = "SELECT ean, name FROM products WHERE category = '$category'";
    $productResult    = $this->mysqli->query($productQuery);

    while ($productRow = $productResult->fetch_object()) {
        $productPriceQuery     = "SELECT ean, price+shipmentCost AS totalPrice FROM prices WHERE ean = $productRow->ean ORDER BY totalPrice ASC";
        $productPriceResult = $this->mysqli->query($productPriceQuery);

        while($row=$productPriceResult->fetch_object()) {
        $ProductPrice = $row->totalPrice;
        $ProductURL = $row->productURL; //I'm not seeing where you're querying for the URL so you'll have to append accordingly.
            }
    }

And then Create your table you're talking about.

   echo "<table><th>Product Price</th><th>Product URL</th>";
    echo "<tr><td>" . $ProductPrice . "</td><td>" . $ProductURL . "</td></tr>";
    echo "</table>";
}

Hopefully there's some help on where to start there. Good luck.

share|improve this answer
    
Do everyone a favor and if you're going to downvote at least tell everyone your reasoning. :) –  Morgan Green Dec 31 '13 at 5:04
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.