supesham Posted November 19, 2010 Share Posted November 19, 2010 Hey everyone, So i was following a tutorial on youtube, it is a simple search and display program using mysql and php. i have completed 4 of 5 tutorials and when i enter a keyword in the textfield search and subit, it displays all the results in my database instead of the one. here is my code, any help would be very helpful thanks. $button = $_GET['submit']; $search = $_GET['search']; if(strlen($search)<2) echo "Search term too short"; else { echo "You searched for <b>$search</b><hr size='1'>"; mysql_connect("localhost", "root", "supersham"); mysql_select_db("carbreaker"); $keywords = $getrow['keywords']; //explode our search term $search_exploded = explode(" ", $search); $search_each = 0; while ($search_each < count($search_exploded)) { if ($search_each == 0) { $construct = "keywords LIKE '%" . $search_each . "%'"; } else { $construct .= " AND keywords LIKE '%" . $search_each . "%'"; } $search_each++; } //echo out construct $construct = "SELECT * FROM stock WHERE $construct"; $run = mysql_query($construct); $foundnum = mysql_num_rows($run); if($foundnum==0) echo "No Stock Found"; else{ echo "$foundnum results found<br>"; while($runrows = mysql_fetch_assoc($run)) { $make = $runrows['make']; $model = $runrows['model']; $year = $runrows['year']; $cc= $runrows['cc']; $fuel = $runrows['fuel']; $doors = $runrows['doors']; $body = $runrows['body']; $date = $runrows['date']; echo "<b>$make</b> $model $year $cc $fuel $doors $body $date<br>"; } } } ?> Quote Link to comment Share on other sites More sharing options...
Psycho Posted November 19, 2010 Share Posted November 19, 2010 Wow, that's some pretty rough code. I can't even follow what you are doing in some places. For example: $keywords = $getrow['keywords']; I don't see $getrow defined anywhere. Anyway, I think the problem is the consrtuction of your WHERE clause. You could have validated this easily enough by simply echoing the query to the page. It looks as if you are using the counter as the seach term not the actual search terms. Plus, there is a problem in the logic if you were to have two consecutive spaces. But, you are making it much more difficuly than it needs to be with all those loops. $button = trim($_GET['submit']); $search_phrase = trim($_GET['search']); if(strlen($search_phrase)<2) { echo "Search term too short"; } else { echo "You searched for <b>$search_phrase</b><hr size=\"1\">\n"; mysql_connect("localhost", "root", "supersham"); mysql_select_db("carbreaker"); $keywords = $getrow['keywords']; //explode our search term $search_words = explode(" ", $search_phrase); $like_parts = array(); foreach($search_words as $word) { if($word<>'') { $like_parts[] = "keywords LIKE '%" . mysql_real_escape_string($word} . "%'"; } } $where_clause = (count($like_parts)>0) ? "WHERE ".implode(' OR ', $like_parts) : ''; //echo out construct $construct = "SELECT * FROM stock $where_clause"; $run = mysql_query($construct); $foundnum = mysql_num_rows($run); if($foundnum==0) { echo "No Stock Found"; } else { echo "$foundnum results found<br />\n"; while($row = mysql_fetch_assoc($run)) { echo "<b>{$row['make']}</b> {$row['model']} {$row['year']} {$row['cc']} {$row['fuel']} {$row['doors']} {$row['body']} {$row['date']}<br />\n"; } } } Quote Link to comment Share on other sites More sharing options...
supesham Posted November 19, 2010 Author Share Posted November 19, 2010 Thanks for your time mjdamato, I might just try start all over again. I'm a complete beginner so pretty much all of this is new to me. Tryin to Thanks a million though. Dave 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.