0

Edit- >>>>>> Ok this works fine if I use another browser but I am trying to use it in chrome and I have this error. How comes it is like this? I am not understanding on what I can do. I have looked at the other question to help but still not understanding!

I am creating a shopping cart following a tutorial on youtube, hes using mysql but I have to mysqli for the work I am doing. I keep getting the following error:

Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given in C:\wamp\www\Shoppingcart\index.php on line 35

My code for the page is as follows:

<html>
    <head>
        <link rel="stylesheet" href="css/reset.css">
        <link rel="stylesheet" href="css/style.css">
        <title>Shopping Cart</title>
    </head>
    <body>
        <div id="container">
            <div id="main"><?php require ($page . ".php"); ?></div>
            <div id="sidebar"><h1>Cart</h1>
            <?php
                if(isset($_SESSION['cart'])){
                    $sql = "SELECT * FROM products WHERE id_products IN(";
                    foreach($_SESSION['cart'] as $id => $value){
                        $sql .= $id. ",";
                    }
                    $sql = substr($sql, 0,-1) . ") ORDER BY id_products ASC";
                    $query = mysqli_query($connction, $sql);

                    while($row = mysqli_fetch_array($query)){
            ?>
                <p><?php echo $row['name']; echo " x " . $_SESSION['cart'][$row['id_products']]['quantity'];?></p>

            <?php 
                    }
                }else{
                    echo "<p>Your cart is empty.<br> Please add some items</p>";
                }
                echo '<a href="index.php?page=cart">Go to cart</a>';
                ?>

            </div>
        </div>
    </body>
 </html>

Line 35 where the error occurs in the code is:

while($row = mysqli_fetch_array($query)){

Can anyone give me an idea what is causing it I have tried asking the person who created the video but as it was made roughly 6 years ago he is most probably not there.

Many thanks

LizzyPooh
  • 975
  • 1
  • 8
  • 11
  • It seems it doesn't return a mysqli_result, so it can't handle it. Maybe try to check if it is something... `if (mysqli_result) { ... loop and other stuff ... }` - yes, pretty basic, don't shoot me for it.. And you may want to try `implode(',', array_keys($_SESSION['cart']))` for your own convenience... Anyway, it's probably because your query failed... – Raphioly-San Mar 19 '15 at 14:01
  • 1
    You appear to have a typo for the variable $connection (it says $connction), if it is spelt correctly in your actual database connection script, that is. Have you actually connected to the database at all? – paddyfields Mar 19 '15 at 14:01
  • your `$query = mysqli_query($connction, $sql);` is returning false. Probably your SQL Statement has a syntax error – bcesars Mar 19 '15 at 14:03
  • simple your $query is not an resultset object, it's a boolen value . just write echo $query; and it will give you output either 0 or 1. ths is the problem – Anant Kumar Singh Mar 19 '15 at 14:03
  • 1
    @paddyfields I think you got something there as well – Raphioly-San Mar 19 '15 at 14:05
  • @paddyfields Thank you so much it brought out so many errors everywhere thanks!! still got this error though – LizzyPooh Mar 19 '15 at 14:07

1 Answers1

0

This is because your query is returning an error. Most likely, there is no IDs in your session cart array, so your query has ended up like this:

SELECT * FROM products WHERE id_products IN() ORDER BY id_products ASC;

You can call mysqli_error() to see the exact error, after checking to see whether or not mysqli_query() has returned false.

Alex
  • 3,009
  • 3
  • 21
  • 46