Jump to content

[SOLVED] Creating a Simple MySQL Search Using PHP


KGK1027

Recommended Posts

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!

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.

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!

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/

 

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!

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.