KGK1027 Posted March 10, 2009 Share Posted March 10, 2009 Ok, let's get this out of the way: I am not a programmer. I volunteered to redesign a website for a small non-profit, but they would like to implement these additional search features, and I'm not extremely well-versed in PHP. They are hosting with GoDaddy and we have set up a MySQL database. I would like to know how to create a search feature so that website visitors can type in a search query, select the category they would like to search in, (First Name, for example) and pull results from the database. I have found several PHP codes but have been having trouble implementing them (not sure if it's me or GoDaddy). I have been able to create a form where users add information to the database, so I know my server name, etc. are correct. Any ideas? Thanks in advance! Quote Link to comment https://forums.phpfreaks.com/topic/148791-solved-creating-a-simple-mysql-search-using-php/ Share on other sites More sharing options...
ngreenwood6 Posted March 10, 2009 Share Posted March 10, 2009 Without seeing any code it is kinda difficult to help you but normally when you want to create a search you will need to know what table/fields you want to search and you will more than likely need to use mysql's "LIKE" in your query. Hopefully that will give you something to think about and something to look into. Quote Link to comment https://forums.phpfreaks.com/topic/148791-solved-creating-a-simple-mysql-search-using-php/#findComment-781284 Share on other sites More sharing options...
KGK1027 Posted March 11, 2009 Author Share Posted March 11, 2009 I found some code online but now I'm having trouble even connecting to the database! Here's my code: search.html <html> <head> <title>Catalog Search</title> </head> <body> <h1>Search</h1> <form method="post" action="results.php">Choose Search Type:<br /><select name="searchtype"> <option value="firstname">First Name:</option> <option value="lastname">Last Name:</option> <option value="color">Favorite Color:</option></select> <br /> Enter Search Term:<br /><input name="searchterm" size="40" /> <br /><input name="submit" value="Search" type="submit" /> </form> </body> </html> And the PHP - results.php <html> <head> <title>Search Results</title> </head> <body> <h1>Search Results</h1> <?php // create short variable names $searchtype=$_POST['searchtype']; $searchterm=trim($_POST['searchterm']); if (!$searchtype || !$searchterm) { echo 'You have not entered search details. Please go back and try again.'; exit; } if (!get_magic_quotes_gpc()){ $searchtype = addslashes($searchtype); $searchterm = addslashes($searchterm); } @ $db = new mysqli('Host', 'User', 'Password', 'table'); if (mysqli_connect_errno()) { echo 'Error: Could not connect to database.'; exit; } $query = "select * from table where ".$searchtype." like '%".$searchterm."%'"; $result = $db->query($query); $num_results = $result->num_rows; echo "<p>Number of results found: ".$num_results."</p>"; for ($i=0; $i <$num_results; $i++) { $row = $result->fetch_assoc(); echo "<p><strong>".($i+1).". First Name: "; echo htmlspecialchars(stripslashes($row['firstname'])); echo "</strong><br />Last Name: "; echo stripslashes($row['lastname']); echo "<br />Favorite Color: "; echo stripslashes($row['color']); echo "<br />Gender: "; echo stripslashes($row['gender']); echo "</p>"; } $result->free(); $db->close(); Getting the "error: could not connect to database" Any ideas? I'm pulling my database name, host, etc... straight from phpmyadmin, so I don't think those are wrong. I could really use some advice on this! Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/148791-solved-creating-a-simple-mysql-search-using-php/#findComment-781750 Share on other sites More sharing options...
cortez Posted March 11, 2009 Share Posted March 11, 2009 Hey I've just been playing with my first php search engine as well. I've been working from this tutorial. It's a little old but I found it very useful. http://www.devshed.com/c/a/PHP/Creating-a-Searchable-Inventory-System-Setting-Up-Your-Database-and-User-Interface/ Quote Link to comment https://forums.phpfreaks.com/topic/148791-solved-creating-a-simple-mysql-search-using-php/#findComment-781797 Share on other sites More sharing options...
racer x Posted March 11, 2009 Share Posted March 11, 2009 You may need to check if your godaddy host is set up to use PHP 5. They give you an option when you sign up to use 4 or 5 and I think mysqli_connect_errno() is only in PHP 5. Quote Link to comment https://forums.phpfreaks.com/topic/148791-solved-creating-a-simple-mysql-search-using-php/#findComment-781799 Share on other sites More sharing options...
KGK1027 Posted March 11, 2009 Author Share Posted March 11, 2009 Thanks racer x - I am using MySQL 4.1, so I'll try that code with version 5 instead. And cortez - that tutorial looks really good. I'm surprised I hadn't come across it yet! I'll test that out, too, and hopefully I can get something to work! Quote Link to comment https://forums.phpfreaks.com/topic/148791-solved-creating-a-simple-mysql-search-using-php/#findComment-781995 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.