justlukeyou Posted March 2, 2011 Share Posted March 2, 2011 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>£ $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; } ?> Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted March 2, 2011 Share Posted March 2, 2011 How does it "not work"? Quote Link to comment Share on other sites More sharing options...
justlukeyou Posted March 2, 2011 Author Share Posted March 2, 2011 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%'"; Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted March 2, 2011 Share Posted March 2, 2011 That means the query is failing. Echo both the query and mysql_error() upon query failure. Quote Link to comment Share on other sites More sharing options...
justlukeyou Posted March 2, 2011 Author Share Posted March 2, 2011 Hi, If I dont put the Limit 0,10 the query works but it prints everything in the database. Which could be thousands. But if I put the Limit function in like I used to it fails. Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted March 2, 2011 Share Posted March 2, 2011 That means the query is failing. Echo both the query and mysql_error() upon query failure. Quote Link to comment Share on other sites More sharing options...
justlukeyou Posted March 2, 2011 Author Share Posted March 2, 2011 Thanks, I dont know what you mean though. Are you saying I should actually display the query and mysql_error()? I didn't know I could do this, what will this give me? Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted March 2, 2011 Share Posted March 2, 2011 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 . . . Quote Link to comment Share on other sites More sharing options...
justlukeyou Posted March 2, 2011 Author Share Posted March 2, 2011 Hi, Got it work now, I was putting the limit in the wrong place. Now I can go to bed. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.