user3289740
user3289740

Reputation: 351

Trying to print out the values of an Associative Array - Error: Array to string conversion

I'm trying to echo the values of an associative array that contains all the rows in a table using foreach in a table.

Here is the code:

<?php 
    $result = doSql("SELECT * FROM carts WHERE userID =".$userID);
     if($result && mysqli_num_rows($result)>0) {
        $cartRows = mysqli_fetch_all($result); 

             foreach($cartRows as $key => $value) {

                echo "<tr>
                    <td scope='col'>" .$value['cartID']. "</td>
                    <td scope='col'>" .$value["productToCartQty"]. "</td>
                    <td scope='col' colspan='2'>".$value['invoiced']."</td>
                    <td scope='col' colspan='2'>" .$value['customization']."</td>
                   </tr>";
                }
            } 
 ?>

The error: Array to string conversion

I don't understand why I'm getting this error as I am only echoing out the value inside of the array $value i.e. echo $value['cartID']?

Here is the screenshot of the error: https://gyazo.com/cdc8dde8c2e09e91b5209c2b50bcb72c

Any ideas?

Upvotes: 1

Views: 99

Answers (1)

Muhammad Bilal
Muhammad Bilal

Reputation: 2134

You need to change:

$cartRows = mysqli_fetch_all($result);

to:

$cartRows = mysqli_fetch_all($result,MYSQLI_ASSOC);

Upvotes: 1

Related Questions