anevins Posted March 13, 2011 Share Posted March 13, 2011 Hi there, I'm working on a search bar for my website and I want what ever someone types in, to be like the data in my table column name. My table column name is 'make', my table name is 'stick'. At the moment, my code outputs " your search: "..mysearchterm.." returned zero results", even when i type in the same name as a row in my column. Can anyone make sense of why my code's not performing? <form id="search" name="search" id="search" method="get" action="<?php echo $_SERVER['PHP_SELF']; ?>" /> <input type="text" name="terms" value="Search..." /> <input class="button" type="submit" name="Search" value="Search" /> </form> <?php include_once('connectvars.php'); if (isset($_GET['terms']) && ($_GET['terms'] != 'Search...') ) { $terms = $_GET['terms']; $dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME) or die *('Error connecting to MySQL server'); // Query the database. $query = "SELECT sid, type, colour, make FROM stick WHERE make LIKE '%$terms% '"; // Fetch the results. $result=mysqli_query($dbc,$query); // Print the results: $num_rows = mysqli_num_rows($result); // If the results of the query match to 1 or more rows, display the products by their title, price and image. if ($num_rows > 0){ if($result=mysqli_query($dbc,$query)){ $output = Array(); while($row=mysqli_fetch_assoc($result)) { //'<br /> Street: ' .$row['street'].' $output[] = '<ul>'; $output[] = '<li> Type: '.$row['make'] .' Size: '.$row['type'].'<br />Colour: '.$row['colour'] . '" /></li>'; $output[] = '</ul>'; } } echo join('',$output); } // If the results don't match the titles of the table, output this message. else { echo "<h3>Sorry,</h3>"; echo "<p>your search: "" .$terms. "" returned zero results</p>"; } } else { // Tell them to use the search form. echo '<p class="error">Please use the search form at the top of the window to search this site.</p>'; } ?> Thanks. Link to comment https://forums.phpfreaks.com/topic/230540-search/ Share on other sites More sharing options...
anevins Posted March 13, 2011 Author Share Posted March 13, 2011 N/A Link to comment https://forums.phpfreaks.com/topic/230540-search/#findComment-1187104 Share on other sites More sharing options...
anevins Posted March 13, 2011 Author Share Posted March 13, 2011 Found out what it was. Line: $query = "SELECT * FROM stick WHERE make LIKE '%$terms% '"; Problem: '%$terms% '"; (space bar after right percentage) Solution: '%$terms%'"; (no space bar) Link to comment https://forums.phpfreaks.com/topic/230540-search/#findComment-1187110 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.