查询无效。介意看看吗?

时间:2016-05-10 21:22:15

标签: php mysql sql

我一直在:

  

警告:mysqli_fetch_assoc()期望参数1为mysqli_result,布尔值在

中给出

我已经列出了下面的问题sql,我想知道我做错了什么($ status是一个字母表,只是说“表格已提交”)

$sql= "SELECT * FROM `veggie` ORDER BY `buy_date` desc LIMIT 20 OFFSET 0 WHERE `status` = '$status' ";
编辑:可以做。第50和84行

<?php
require_once $_SERVER['DOCUMENT_ROOT'].'/system/init.php';
if (!is_logged_in()) {
  login_error_redirect();
}
include 'includes/head.php';
include 'includes/navigation.php';

$page='';
$status = "Form Submitted";
if (!isset($_GET['page'])){
  $sql= "SELECT * FROM `veggie` ORDER BY `buy_date` desc LIMIT 20 OFFSET 0 WHERE `status` = '$status' ";
}

if(isset($_GET['page'])){
  $page = $_GET['page'];
  $offset = 20 * $page;
  $sql= "SELECT * FROM veggie ORDER BY buy_date desc LIMIT 20 OFFSET $offset WHERE `status` = '$status' ";
}
$result = $db->query($sql);

$p = $page;

?>


<h2 class="text-center">Veggie Crate Forms</h2><hr>

<table class= "table table-bordered table-striped table-auto">
<thead>

  <th class="info">Full_Name</th>
  <th class="info">email</th>
  <th class="success">crate</th>
  <th class="active"># people</th>
  <th class="active">Products</th>
  <th class="active">Bulk Items</th>
  <th class="active">Interests</th>
  <th class="active">How did you find us?</th>
  <th class="active">Are you ready?</th>
<!--  <th class="danger">Delete</th> -->

</thead>
<tbody>
  <?php


    ?>

    <?php while($archived = mysqli_fetch_assoc($result)) : ?>

      <?php
      $full_name = $archived['first'].' '.$archived['last'];
       ?>
    <tr>

      <td><?=$full_name?></td>
      <td><?=$archived['email']?></td>
      <td><?=$archived['crate']?></td>
      <td><?=$archived['people']?></td>
      <td><?=$archived['bulk']?></td>
      <td><?=$archived['class']?></td>
      <td><?=$archived['how']?></td>
      <td><?=$archived['ready']?></td>
      <!-- <td><a href="archived.php?delete=<?=$archived['id']?>"><span class="glyphicon glyphicon-minus">Delete</span></a></td> -->
    </tr>
    <?php  endwhile; ?>
</tbody>
</table>


<div id="decrease" style="" class="">


<?php if ($page > 0 ): ?>
    <form class="form" action="whf.php?page=<?=--$p?> " method="post">
  <input type="submit" name="name" value="<--">
<?php endif; ?>

  </div>

  <div id="increase" class="">

    <?php   $countr = mysqli_num_rows($result);

    if($countr == 10){


     if ($page == '' || $page < 1 ): ?>
        <form class="form" action="whf.php?page=1 " method="post">
      <input type="submit" name="name" value="-->">
    <?php endif; ?>

  <?php

  if ($page < $countr && $page != '' && $page != 0 ): ?>
      <form class="form" action="whf.php?page=<?=++$p?> " method="post">
    <input type="submit" name="name" value="-->">
  <?php endif;

} ?>


    </div>



 <?php include 'includes/footer.php';  ?>

3 个答案:

答案 0 :(得分:2)

您的orderlimit位置错误。

http://dev.mysql.com/doc/refman/5.7/en/select.html

尝试:

SELECT * FROM `veggie` WHERE `status` = '$status' ORDER BY `buy_date` desc LIMIT 20 OFFSET 0

您还应该考虑使用prepared statements

答案 1 :(得分:0)

第一。 WHERE子句位于ORDER子句

之前

第二。阅读警告,你给它错误的参数。

$link = mysqli_connect("localhost", "username", "password", "database");
$result = mysqli_query($link, $sql)
$row = mysqli_fetch_assoc($result);

第三。为什么你使用面向对象然后切换到程序?? 如果你开始

$res = $db->query($sql);

然后它应该是:

$row = $res->fetch_assoc();

查看手册 http://php.net/manual/en/mysqli-result.fetch-assoc.php

答案 2 :(得分:0)

您有多种语法错误。 您的查询应如下所示:

SELECT * FROM `veggie`  WHERE `status` = "$status"  ORDER BY `buy_date` desc LIMIT 20 OFFSET 0

ORDER BY,LIMIT和OFFSET将在查询结束时进行。