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. Quote Link to comment 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. Quote Link to comment 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. Quote Link to comment 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>'; } ?> Quote Link to comment 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>'; } } ?> Quote Link to comment 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. Quote Link to comment 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>'; } } ?> Quote Link to comment 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. ??? Quote Link to comment 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 Quote Link to comment 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. Quote Link to comment 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. Quote Link to comment 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 Quote Link to comment 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. Quote Link to comment 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 Quote Link to comment 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. Quote Link to comment 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. Quote Link to comment 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" Quote Link to comment 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. Quote Link to comment 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! 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.