Jump to content

Adding Limit To Code?


justlukeyou

Recommended Posts

I had a piece of code which limited the number of items printed from a database.  However, I can no longer get it to work. 

 

$query = "SELECT * FROM productfeed WHERE description like '%$description%' LIMIT 0, 3";

 

This is the code I am trying to limit, but I cant get it to work.

 

<?php

ini_set('display_errors', 1);
error_reporting(-1);



$query = "SELECT * FROM productfeed";

if(isset($_GET['description']) && !empty($_GET['description'] ))
{
$description = $_GET['description'];
$query .= " WHERE description like '%$description%'";
}

if(isset($_GET['price']) && !empty($_GET['price']))
{
$price = explode('-', $_GET['price']);
$lowPrice = (int)$price[0];
$highPrice = (int)$price[1];

$query .= " AND price BETWEEN $lowPrice AND $highPrice";
}


$result = mysql_query($query);

while($row = mysql_fetch_assoc($result))

{

$id = $row['id'];
$image = $row['awImage'];
$link = $row['link'];
$description = $row['description'];
$fulldescription = $row['fulldescription'];
$price = $row['price'];

echo "<div class='productdisplayshell'>
<div class='productdisplayoutline'>
<div class='productborder'><center>
<a href='$link' target='_blank'><img src='$image' width=\"95%\" /></a>
</center> </div></div>
<div class='productdescriptionoutline'>
<div class='productdescriptionbox'>
<a href='$link' target='_blank' >$description</a>
</div>
<div class='productfulldescriptionbox'>$fulldescription</div>
</div>
<div class='productpriceoutline'>
<div class='productpricebox'>
<center>&#163; $price</center>
</div>
<div class='productbuybutton'>
<center><a href='$link' target='_blank' ><img src=/images/buybutton.png /></a></center>
</div>
</div>
</div>";
} 

if ($_GET['description'] == $description ) {
echo 'Sorry, this product is not available.  Please visit our <a href="http://www.ukhomefurniture.co.uk">Homepage</a>.';
}


?>

<?php
function sanitizeString($description)
{
$description = mysql_real_escape_string($description);
$description = stripslashes($description);
$description = htmlentities($description);
return $var;

$price = mysql_real_escape_string($price);
$price = stripslashes($price);
$price = htmlentities($price);
return $var;


}
?> 

Link to comment
https://forums.phpfreaks.com/topic/229427-adding-limit-to-code/
Share on other sites

Hi,

 

The following code used to work but I have changed this query:

 

$query = "SELECT * FROM productfeed WHERE description like '%$description%' LIMIT 0, 3";

 

However, if I add LIMIT 0, 3 to this or anyother query it creates an error

 

$query .= " WHERE description like '%$description%' LIMIT 0, 3";

 

mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource

 

$query .= " WHERE description like '%$description%'";

Yes. It will give you the entire query string, and the error that mysql generates. By comparing the two, you should be able to spot the problem.

 

if( !$result = mysql_query($query) ) {
     echo "<br>Query string: $query<br>Produced error: " . mysql_error() . '<br>';
}

 

EDIT: Closed BBCode tags . . .

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.