Cjfidler Cjfidler - 6 months ago 10
PHP Question

Query will not work. Mind Taking a look at it?

I keep getting:


Warning: mysqli_fetch_assoc() expects parameter 1 to be mysqli_result, boolean given in


I've listed the sql in question below and am wondering what im doing wrong ($status is a string that just says "Form Submitted)

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


edit: can do. line 50 and 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'; ?>

Answer

Your order and limit are in the wrong place.

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

Try:

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

You also should look into using prepared statements.

Comments