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
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%'";

Link to comment
Share on other sites

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 . . .

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.