artweb Posted February 6, 2008 Share Posted February 6, 2008 I need a search box for my website. So that a user can come and find things on my site just by typing in a word. When they click search it will give them a list of pages on my site that has to do with that word they searched for, with the links to the pages. I use mysql and php. I don't want to use google or any pre made search. Link to comment https://forums.phpfreaks.com/topic/89769-solved-search-box-for-website/ Share on other sites More sharing options...
ratcateme Posted February 7, 2008 Share Posted February 7, 2008 Hi if you have a database with all your pages in it you could make a search box like this <table> <?php $con=mysql_connect('host','user','pass'); mysql_select_db('site_info',$con); $query="SELECT * FROM `pages` WHERE `page` LIKE ('%".$_REQUEST['search']."%');"; $result=mysql_query($query,$con); while($row=mysql_fetch_array($result)){ echo ' <tr> <td><a href="'.$row['page_url'].'">'.$row['page_name'].'</a></td> <td>'.$row['page_description'].'</td> </tr>'; } ?> </table> <form action="search.php" method="get"> <center><input type="text" name="search" /><input type="submit" value="Search" /></center> </form> Scott. Link to comment https://forums.phpfreaks.com/topic/89769-solved-search-box-for-website/#findComment-460626 Share on other sites More sharing options...
artweb Posted February 7, 2008 Author Share Posted February 7, 2008 Ok, I did what you said. I made a table in my database called site_info and put alot of my sites urls and page names into it. Then I changed the php to match my database. But I keep getting this error. Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /html/search.php on line 308 here's my code: <?php $con=mysql_connect('databasename','password','username'); mysql_select_db('site_info',$con); $query="SELECT * FROM site_info WHERE page_url,page_names LIKE ('%".$_REQUEST['search']."%');"; $result=mysql_query($query,$con); while($row=mysql_fetch_array($result)){ This line 308 for me echo ' <tr> <td><a href="'.$row['page_url'].'">'.$row['page_name'].'</a></td> </tr>'; } ?> </table> <form action="search.php" method="get"> <center><input type="text" name="search" /><input type="submit" value="Search" /></center> </form> Thank you for your help. Link to comment https://forums.phpfreaks.com/topic/89769-solved-search-box-for-website/#findComment-460996 Share on other sites More sharing options...
Wolphie Posted February 7, 2008 Share Posted February 7, 2008 Try this <?php $con = mysql_connect('databasename', 'password', 'username'); mysql_select_db('site_info', $con); $query = "SELECT * FROM `site_info` WHERE `page_url`, `page_names` LIKE ('%" . $_POST['search'] . "%')"; $result = mysql_query($query, $con) or die('Error: ' . mysql_error()); // Always use error handling while($row = mysql_fetch_array($result)) { This line 308 for me echo ' <tr> <td><a href="' . $row['page_url'] . '">' . $row['page_name'] . '</a></td> </tr> </table>'; } if(isset($_POST['submit'])) { echo ' <form action="' . $_SERVER['PHP_SELF'] . '" method="post"> <center><input type="text" name="search" /><input type="submit" value="Search" name="submit" /></center> </form>'; } ?> Link to comment https://forums.phpfreaks.com/topic/89769-solved-search-box-for-website/#findComment-461008 Share on other sites More sharing options...
Wolphie Posted February 7, 2008 Share Posted February 7, 2008 Oops that's wrong <?php $con = mysql_connect('databasename', 'password', 'username'); mysql_select_db('site_info', $con); if(!isset($_POST['submit'])) { echo ' <form action="' . $_SERVER['PHP_SELF'] . '" method="post"> <center><input type="text" name="search" /><input type="submit" value="Search" name="submit" /></center> </form>'; } else { $query = "SELECT * FROM `site_info` WHERE `page_url`, `page_names` LIKE ('%" . $_POST['search'] . "%')"; $result = mysql_query($query, $con) or die('Error: ' . mysql_error()); // Always use error handling while($row = mysql_fetch_array($result)) { This line 308 for me echo ' <tr> <td><a href="' . $row['page_url'] . '">' . $row['page_name'] . '</a></td> </tr> </table>'; } } ?> Link to comment https://forums.phpfreaks.com/topic/89769-solved-search-box-for-website/#findComment-461018 Share on other sites More sharing options...
artweb Posted February 7, 2008 Author Share Posted February 7, 2008 Ok, I did that, and it helped but now I get a new error. Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ' page_name, LIKE ('%blaster%')' at line 1 My database table is set up like this. CREATE TABLE site_info ( id INT NOT NULL AUTO_INCREMENT PRIMARY KEY, page_url varchar(100), page_name varchar(100) ); Thank you again for all your help. Link to comment https://forums.phpfreaks.com/topic/89769-solved-search-box-for-website/#findComment-461148 Share on other sites More sharing options...
Wolphie Posted February 7, 2008 Share Posted February 7, 2008 Sorry typo, i spelled it "names" rather than "name" Use this: <?php $con = mysql_connect('databasename', 'password', 'username'); mysql_select_db('site_info', $con); if(!isset($_POST['submit'])) { echo ' <form action="' . $_SERVER['PHP_SELF'] . '" method="post"> <center><input type="text" name="search" /><input type="submit" value="Search" name="submit" /></center> </form>'; } else { $query = "SELECT * FROM `site_info` WHERE `page_url`, `page_name` LIKE ('%" . $_POST['search'] . "%')"; $result = mysql_query($query, $con) or die('Error: ' . mysql_error()); // Always use error handling while($row = mysql_fetch_array($result)) { This line 308 for me echo ' <tr> <td><a href="' . $row['page_url'] . '">' . $row['page_name'] . '</a></td> </tr> </table>'; } } ?> Link to comment https://forums.phpfreaks.com/topic/89769-solved-search-box-for-website/#findComment-461153 Share on other sites More sharing options...
artweb Posted February 7, 2008 Author Share Posted February 7, 2008 I seen that too, but my error still is the same. ??? Link to comment https://forums.phpfreaks.com/topic/89769-solved-search-box-for-website/#findComment-461155 Share on other sites More sharing options...
Wolphie Posted February 7, 2008 Share Posted February 7, 2008 <?php $con = mysql_connect('databasename', 'password', 'username'); mysql_select_db('site_info', $con); if(!isset($_POST['submit'])) { echo ' <form action="' . $_SERVER['PHP_SELF'] . '" method="post"> <center><input type="text" name="search" /><input type="submit" value="Search" name="submit" /></center> </form>'; } else { $query = "SELECT * FROM `site_info` WHERE `page_url`, `page_name` LIKE '%" . $_POST['search'] . "%'"; $result = mysql_query($query, $con) or die('Error: ' . mysql_error()); // Always use error handling while($row = mysql_fetch_array($result)) { This line 308 for me echo ' <tr> <td><a href="' . $row['page_url'] . '">' . $row['page_name'] . '</a></td> </tr> </table>'; } } ?> I think it may be the brackets. Try that Link to comment https://forums.phpfreaks.com/topic/89769-solved-search-box-for-website/#findComment-461158 Share on other sites More sharing options...
artweb Posted February 7, 2008 Author Share Posted February 7, 2008 Taking the Brackets didn't change my nice little error. :-\ Thank you for your help, if you know anything else to do, please let me know. Link to comment https://forums.phpfreaks.com/topic/89769-solved-search-box-for-website/#findComment-461171 Share on other sites More sharing options...
Wolphie Posted February 7, 2008 Share Posted February 7, 2008 <?php $con = mysql_connect('databasename', 'password', 'username'); mysql_select_db('site_info', $con); if(!isset($_POST['submit'])) { echo ' <form action="' . $_SERVER['PHP_SELF'] . '" method="post"> <center><input type="text" name="search" /><input type="submit" value="Search" name="submit" /></center> </form>'; } else { $query = "SELECT * FROM `site_info` WHERE `page_url`, `page_name` LIKE '%" . $_POST['search'] . "%'"; $result = mysql_query($query, $con) or die('Error: ' . mysql_error()); // Always use error handling while($row = mysql_fetch_array($result)) { echo ' <tr> <td><a href="' . $row['page_url'] . '">' . $row['page_name'] . '</a></td> </tr> </table>'; } } ?> Try that WITHOUT changing a thing, then copy and paste the error here. Link to comment https://forums.phpfreaks.com/topic/89769-solved-search-box-for-website/#findComment-461178 Share on other sites More sharing options...
artweb Posted February 7, 2008 Author Share Posted February 7, 2008 Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ' `page_name` LIKE '%Airless Blaster%'' at line 1 Link to comment https://forums.phpfreaks.com/topic/89769-solved-search-box-for-website/#findComment-461180 Share on other sites More sharing options...
Wolphie Posted February 7, 2008 Share Posted February 7, 2008 Try removing page_url from the query and try just a single field. Link to comment https://forums.phpfreaks.com/topic/89769-solved-search-box-for-website/#findComment-461187 Share on other sites More sharing options...
craygo Posted February 7, 2008 Share Posted February 7, 2008 you can't put 2 fields to look in 1 like call $query = "SELECT * FROM `site_info` WHERE `page_url` LIKE '%" . $_POST['search'] . "%' OR `page_name` LIKE '%" . $_POST['search'] . "%'"; Also WHERE statements are always seperated by AND, OR not a comma Ray Link to comment https://forums.phpfreaks.com/topic/89769-solved-search-box-for-website/#findComment-461191 Share on other sites More sharing options...
Wolphie Posted February 7, 2008 Share Posted February 7, 2008 Yeah, that's what i thought however i wasn't sure. Thanks for the clarification. Link to comment https://forums.phpfreaks.com/topic/89769-solved-search-box-for-website/#findComment-461197 Share on other sites More sharing options...
artweb Posted February 7, 2008 Author Share Posted February 7, 2008 You guys are the best! Excellent, It works perfect. Thanks for your help. Link to comment https://forums.phpfreaks.com/topic/89769-solved-search-box-for-website/#findComment-461222 Share on other sites More sharing options...
Wolphie Posted February 7, 2008 Share Posted February 7, 2008 Our pleasure, please remember to click "Topic Solved" Link to comment https://forums.phpfreaks.com/topic/89769-solved-search-box-for-website/#findComment-461229 Share on other sites More sharing options...
phpSensei Posted February 7, 2008 Share Posted February 7, 2008 I don't think he has clicked it, if you don't know what the Topic Solved button is, refer to Wolphie's signature. Link to comment https://forums.phpfreaks.com/topic/89769-solved-search-box-for-website/#findComment-461233 Share on other sites More sharing options...
artweb Posted February 7, 2008 Author Share Posted February 7, 2008 You guys are the best! Excellent! It works perfect. Thank you! Link to comment https://forums.phpfreaks.com/topic/89769-solved-search-box-for-website/#findComment-461234 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.