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!

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.