Click here to Skip to main content
15,886,199 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
when run this code the error will show
mysqli_fetch_assoc() expects parameter 1 to be mysqli_result


What I have tried:

<?php
           //unset($_COOKIE['recently_viewed']);
         if(isset($_COOKIE['recently_viewed'])){
             $arrRecentView=unserialize($_COOKIE['recently_viewed']);
             $countRecentView=count($arrRecentView);
             $countStartRecentView=$countRecentView-4;
             if($countRecentView>4){
                 $arrRecentView=array_slice($arrRecentView,$countStartRecentView,4);
             }
              $recentViewid=implode(",",$arrRecentView);
              $res=mysqli_query($conn,"select * from product where id IN ($recentViewid)");

         ?>
       <!-- Recent viewed -->
       <section class="htc__product__area--2 pb--100 product-details-res">
           <div class="container">
               <div class="row">
                   <div class="col-xs-12">
                       <div class="section__title--2 text-center">
                           <h2 class="title__line">Recently Viewed</h2>
                           <p>But I must explain to you how all this mistaken idea</p>
                       </div>
                   </div>
               </div>
               <div class="row">
                   <div class="product__wrap clearfix">
                   <?php
                   if (!$res){
                       die('Invalid query: '.mysql_error());
                    }

                    while ($list=mysqli_fetch_assoc($res)) { ?>


                       <div class="col-md-4 col-lg-3 col-sm-4 col-xs-12">
                               <div class="category">
                                   <div class="ht__cat__thumb">
                                       <a href="product.php?id=<?php echo $list['id']?>">
                                           <img src="<?php echo PRODUCT_IMAGE_SITE_PATH.$list['image']?>" alt="product images">
                                       </a>
                                   </div>
                                   <div class="fr__hover__info">
                                       <ul class="product__action">
                                           <li><a href="javascript:void(0)" onclick="wishlist_manage('<?php echo $list['id']?>','add')">class="icon-heart icons"></a></li>
                                           <li><a href="javascript:void(0)" onclick="manage_cart('<?php echo $list['id']?>','add')">class="icon-handbag icons"></a></li>
                                       </ul>
                                   </div>
                                   <div class="fr__product__inner">
                                       <h4><a href="product.php?id=<?php echo $list['id']?>"><?php echo $list['name']?></a></h4>
                                       <ul class="fr__pro__prize">
                                           <li class="old__prize"><?php echo $list['mrp']?></li>
                                           <li><?php echo $list['price']?></li>
                                       </ul>
                                       <div class="cr__btn">
                                           <a href="javascript:void(0)" onclick="manage_cart('<?php echo $list['id']?>','add')">Add to Cart</a>
                                       </div>
                                   </div>
                               </div>
                           </div>
                   <?php } ?>
                   </div>
               </div>
           </div>
       </section>
       <?php
       $arrRec=unserialize($_COOKIE['recently_viewed']);
       if(($key=array_search($product_id,$arrRec))!==false){
           unset($arrRec[$key]);
       }
       $arrRec[]=$product_id;
       setcookie('recently_viewed',serialize($arrRec),time()+60*60*24*365);
        }else{
           $arrRec[]=$product_id;
           setcookie('recently_viewed',serialize($arrRec),time()+60*60*24*365);
        }
       ?>
       <!-- End Recent viewed -->
Posted
Updated 27-May-21 21:34pm
Comments
Richard MacCutchan 28-May-21 3:42am    
Food for thought: The Lounge[^].

1 solution

mysqli_fetch_assoc() expects parameter 1 to be mysqli_result - Google Search[^]

Only 1.46 million results to choose from!

Or pick any of the "related questions" on the right-hand side of this page to view some of the many many times this question has been asked.

Or try reading the documentation:
Return Values
For SELECT, SHOW, DESCRIBE, EXPLAIN and other statements returning resultset, mysql_query() returns a resource on success, or false on error.


Also, your code is vulnerable to SQL Injection[^]. NEVER use string concatenation to build a SQL query. ALWAYS use a parameterized query.
PHP: SQL Injection - Manual[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900