squigs Posted April 4, 2011 Share Posted April 4, 2011 I've been trying to adapt the code from a tutorial on PhpFreaks to suit my needs for a simple pages search but upon modification I can't seem to get it to display results when using the SQL WHERE clause. I will post the code below, it displays my DB results without the where clause but for some reason I can't make it work using the like statement have a look... // Set up our error check and result check array $error = array(); $results = array(); // First check if a form was submitted. // Since this is a search we will use $_GET if (isset($_GET['title'])) { $searchTerms = trim($_GET['title']); $searchTerms = strip_tags($searchTerms); // remove any html/javascript. if (strlen($searchTerms) < 3) { $error[] = "Search terms must be longer than 3 characters."; }else { $searchTermDB = mysql_real_escape_string($searchTerms); // prevent sql injection. } // If there are no errors, lets get the search going. if (count($error) < 1) { $searchSQL = mysql_query("SELECT * FROM inventory WHERE item_name LIKE '$searchTermDB' OR item_desc LIKE '$searchTermDB' "); $searchResult = $searchSQL or trigger_error("There was an error.<br/>" . mysql_error() . "<br />SQL Was: {$searchSQL}"); if (mysql_num_rows($searchResult) < 1) { $error[] = "The search term provided {$searchTerms} yielded no results."; }else { $results = array(); // the result array $i = 1; while ($row = mysql_fetch_assoc($searchResult)) { $results[] = "{$i}: {$row['item_name']}<br />{$row['low_price']}<br />{$row['high_price']}<br /><br />"; $i++; } } } } function removeEmpty($var) { return (!empty($var)); } ?> <html> <title>My Simple Search Form</title> <style type="text/css"> #error { color: red; } </style> <body> <?php echo (count($error) > 0)?"The following had errors:<br /><span id=\"error\">" . implode("<br />", $error) . "</span><br /><br />":""; ?> <form method="GET" action="<?php echo $_SERVER['PHP_SELF'];?>" name="searchForm"> Search For: <input type="text" name="title" id="title" value="<?php echo isset($searchTerms)?htmlspecialchars($searchTerms):''; ?>" /><br /> <br /><br /> <input type="submit" name="submit" value="Search!" /> </form> <?php echo (count($results) > 0)?"Your search term: {$searchTerms} returned:<br /><br />" . implode("", $results):""; ?> </body> </html> Thank you Quote Link to comment https://forums.phpfreaks.com/topic/232677-what-is-the-matter-with-this-search-code/ Share on other sites More sharing options...
fenway Posted April 5, 2011 Share Posted April 5, 2011 Echo the actual query. Quote Link to comment https://forums.phpfreaks.com/topic/232677-what-is-the-matter-with-this-search-code/#findComment-1197164 Share on other sites More sharing options...
squigs Posted April 5, 2011 Author Share Posted April 5, 2011 Thanks for the reply, I got the code working decently, I wound up changing while ($row = mysql_fetch_assoc($searchResult)) //to this while ($row = mysql_fetch_array($searchResult)) It works now but if you could tell me why that would be even better! Thanks Quote Link to comment https://forums.phpfreaks.com/topic/232677-what-is-the-matter-with-this-search-code/#findComment-1197349 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.