T
T
Tati_922020-05-13 14:04:34
PHP
Tati_92, 2020-05-13 14:04:34

I am newbie! The ORDER BY function has been applied, when the page is refreshed, it does not display the product from the database, but when you click on sorting, it displays everything. What is the error?

Learning from video! create an online store with a database. When going to the page, the browser gives an error:

Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, boolean given in W:\domains\korea.ru\for_face.php on line 89

Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, boolean given in W:\domains\korea.ru\for_face.php on line 132

When you click on sorting, products are displayed, when you click on page views (block and inline), it is also displayed.
Please help me figure out what is wrong!

<!DOCTYPE html>

<html >
 <head>
    <?php
    include('includes/db.php');//Подключаю БД
  
  $sorting='';
    if(isset($_GET['sort'])) {
        $sorting = $_GET['sort'];
            }
    switch ($sorting) {
    
    case 'price-asc';
    $sorting = 'price ASC';
    $sort_neme = 'От дешовых к дорогим';
    break;
    
    case 'price-desc';
    $sorting = 'price DESC';
    $sort_neme = 'От дорогих к дешовым';
    break;
    
    case 'popular';
    $sorting = 'count DESC';
    $sort_neme = 'Популярные';
    break;
    
    case 'news';
    $sorting = 'datetime DESC';
    $sort_neme = 'Новинки';
    break;
    
    case 'brand';
    $sorting = 'brand DESC';
    $sort_neme = 'Бренд';
    break;
    
        default:
    $sorting = 'products_id DESC';
    $sort_neme = 'Нет сортировки';
        break;		
  }
  
  ?>

  <meta charset="utf-8"/>
 
  <meta name="viewport" content="width=device-width,initil-scale=1"/> <!-- ДЛЯ МОБИЛЬНЫХ УСТРОЙСТВ -->
  
  <link href="css/stil.css" rel="stylesheet" type="text/css"/>  <!-- ССЫЛКА НА CSS -->
  <link href="css/media.css" rel="stylesheet" type="text/css"/>  <!-- ССЫЛКА НА CSS -->

 
  
  <title>Косметика для лица</title><!-- Название веб-страницы -->

  <?php require "header.php"; ?>
  
  </head> 
  <div  id="topbutton">&#8593;<br/>Наверх</div> 
 
 <body>
  
  
  <div id="conteiner">   
  <div id="conteiner-goods">
  
  <p id="view">Вид:</p>
  <a href="#"> <img  id="box_list1" src="image/nav/box-list.png" alt="img"/></a>
  <a href="#"> <img  id="box_list2" src="image/nav/box1.png" alt="img"/></a>
  
  <p id="sort">Cортировать:</p>
  <a id="link"><?php echo $sort_neme; ?></a>
  <div id="sort_block">
  <a href="for_face.php?sort=price-asc">От дешовых к дорогим</a><br>
  <a href="for_face.php?sort=price-desc">От дорогих к дешовым</a><br>
  <a href="for_face.php?sort=popular">Популярные</a><br>
  <a href="for_face.php?sort=news">Новинки</a><br>
  <a href="for_face.php?sort=brand">Бренд</a>
  </div>
  
 <div id="single_goods_block"> 
 
 <?php
 
 $result =  mysqli_query($link, "SELECT * FROM  teble_products  ORDER BY  $sorting");//выводим товары
    
  if (mysqli_num_rows($result) > 0) {
    $row = mysqli_fetch_array($result);
    do {
      echo('
   
   
   <div id="goods_block">
      <img class="img_face" src="image/goods/fase/'.$row["image"].'"/>
    
    <p class="style_title"><a href="#">' . $row["title"] . '</a> </p> 
    <hr>
      
    <a data-tooltip="Просмотры"><img id="reviews" src="image/nav/eye.png"/><p id="reviews_quantity">0</p></a>
    
    <a data-tooltip="Добавить в Избранные"><p id="heart_quantity">0</p> <img id="heart" src="image/nav/heart.png"/></a>
    
    <p class="price_goods_face"><strong> '.$row["price"].' </strong> руб.</p> 
    
    <a href="http://korea.ru/cart.php" data-tooltip="Корзина">
    <img  id="cart_goods_face"src="image/Box pink-256.png" alt="img"/>
    <p id="cart_quantity">0</p>
    </a> 
    
    
    <div class="mini_description"> '.$row["mini_description"].'</div>
    </div>     
    	
    
    ');
    }
    while ($row = mysqli_fetch_array($result));
  }
 ?>

   </div>
 

 <div id="single_goods_line"> 
  
 <?php
 
 $result =  mysqli_query($link, "SELECT * FROM  teble_products ORDER BY  $sorting");//выводим товары
    
  if (mysqli_num_rows($result) > 0) {
    $row = mysqli_fetch_array($result);
    do {
    
  
      echo('
   
   
   <div id="goods_block_line">
      <img class="img_face_line" src="image/goods/fase/'.$row["image"].'"/>
    
    <p class="style_title_line"><a href="#">' . $row["title"] . '</a> </p> 
    
      
    <a data-tooltip="Просмотры"><img id="reviews_line" src="image/nav/eye.png"/><p id="reviews_quantity_line">0</p></a>
    
    <a data-tooltip="Добавить в Избранные"><p id="heart_quantity_line">0</p> <img id="heart_line" src="image/nav/heart.png"/></a>
    
        <a href="http://korea.ru/cart.php" data-tooltip="Корзина">
    <img  id="cart_goods_face_line"src="image/Box pink-256.png" alt="img"/>
    <p id="cart_quantity_line">0</p>
    </a>
    
    <p class="price_goods_face_line"><strong> '.$row["price"].' </strong> руб.</p> 
    
    <div class="description_line"> '.$row["description"].'</div>
    </div>     
    	
    
    ');
    }
    while ($row = mysqli_fetch_array($result));
  }
 ?>


   </div>
   </div>
</div>
   
   <script type="text/javascript" src="js/jquery-3.4.1.min.js"></script> <!--  подключается первым -->
   <script type="text/javascript" src="js/jquery.cookie.js"></script>
   <script type="text/javascript" src="js/goods.js"></script>
  
   <script type="text/javascript" src="js/script.js"></script><!--подключается вторым-->
   
 </body> 
  <?php include "footer.php"; ?>

 
</html>


$('document').ready(function(){
  
  $("#box_list1").click(function(){//блок
    
    $("#single_goods_block, #goods_block").show();//показать
    $("#single_goods_line, #goods_block_line").hide();//скрыть
    
    $.cookie('select-style','block');
    
  });
  
  $("#box_list2").click(function(){//список
  
    $("#single_goods_line, #goods_block_line").show();//Показать 
    $("#single_goods_block, #goods_block").hide();//скрыть
    
        $.cookie('select-style','line');
  });
    
  

    $("#link").click(function(){
  
        $("#sort_block").slideToggle(200);
  
  
    }); 
});

Answer the question

In order to leave comments, you need to log in

4 answer(s)
F
FanatPHP, 2020-05-13
@Tati_92

Request error.
To find out which one, before connecting to the database, there should always be a line

mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);

and then php will tell you in detail what is wrong with your request
For a beginner, by the way, the code is not so bad.

I
Ivan Ivanov, 2020-05-13
@maksim_fix

Errors say that a boolean value gets into mysqli_num_rows, but there should be a mysqli_result object. So something is wrong with $result
Read the documentation https://www.php.net/manual/en/function.mysql-num-r...
An example from there:

$link = mysql_connect("localhost", "mysql_user", "mysql_password");
mysql_select_db("database", $link);

$result = mysql_query("SELECT * FROM table1", $link);
$num_rows = mysql_num_rows($result);

N
ninja op op, 2020-05-13
@kur4chyt

Вместо mysqli_num_rows($result)
Используйте $result->num_rows

It helped me.

T
Tati_92, 2020-05-13
@Tati_92

Fatal error: Uncaught exception 'mysqli_sql_exception' with message 'Unknown column 'products_id' in 'order clause'' in W:\domains\korea.ru\for_face.php:89 Stack trace: #0 W:\domains\korea.ru \for_face.php(89): mysqli_query(Object(mysqli), 'SELECT * FROM ...') #1 {main} thrown in W:\domains\korea.ru\for_face.php on line 89
that's what it says! Explain, please!

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question