Menu
Can someone provide me a sample code how I can have a “search” field search for any keyword that matches my records in my database.
I have a list of agencies we fund, they have a 25 word description column. If someone searches for any word and it matches that of the “description” column list records by ID desc.
Is this possible to do a simple search query? find word and/or and list records, otherwise return sorry, no records showing that keyword.
Thanks
[code=php]
$search = $_POST['search_string'];
$query = "SELECT * FROM table_name
WHERE description
LIKE '%$search%'";
$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_assoc($result))
{
# echo columns of interest in desired sequence and format
}
[/code]
[code=php]
$query = "SELECT * FROM table_name
WHERE description
LIKE '%$search%' OR name
LIKE '%$search%'";
[/code]
[code=php]
# convert to upper case, trim it, and replace spaces with "|":
$search = strtoupper(preg_replace('/s+/', '|', trim($_POST['search_string'])));
# create a MySQL REGEXP for the search:
$regexp = "REGEXP '^.*($search).*$'";
$query = "SELECT * FROM table_name
WHERE UPPER(description
) $regexp " .
"OR UPPER(name
) $regexp";
$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_assoc($result))
{
# echo columns of interest in desired sequence and format
}
[/code]
[code=php]
$regexp = "REGEXP '^.*[[:<:]]($search)[[:>:]].*$'";
[/code]
[code=php]
$regexp = "REGEXP '[[:<:]]($search)[[:>:]]'";
[/code]
[code=php]
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html lang='en'>
<head>
<META HTTP-EQUIV='Content-Type' CONTENT='text/html; charset=ISO-8859-1'>
<title>Page title</title>
<style type="text/css">
table {
border-collapse: collapse;
border: solid 1px black;
}
td {
padding: 2px 6px;
border: solid 1px black;
}
</style>
</head>
<body>
<form action='<?php echo $_SERVER['PHP_SELF']; ?>' method='post'>
<p>Search for: <input type='text' name='search' size='20' maxlength='64'></p>
<p><input type='submit' value='Search'></p>
</form>
<?php
if(isset($_POST['search']))
{
$connx = mysql_connect('localhost', '*****', '*****') or die("connx");
$db = mysql_select_db('test') or die(mysql_error());
# convert to upper case, trim it, and replace spaces with "|":
$search = (ini_get('magic_quotes_gpc')) ? stripslashes($_POST['search']) :
$_POST['search'];
$search = mysql_real_escape_string($search);
$search = strtoupper(preg_replace('/s+/', '|', trim($_POST['search'])));
# create a MySQL REGEXP for the search:
$regexp = "REGEXP '[[:<:]]($search)[[:>:]]'";
$query = "SELECT * FROM users
WHERE UPPER(description
) $regexp OR ".
"name
$regexp";
$result = mysql_query($query) or die($query . " - " . mysql_error());
echo "<table>n";
while($row = mysql_fetch_assoc($result))
{
echo "<tr>";
foreach($row as $key => $value)
{
echo "<td>$value</td>";
}
echo "</tr>n";
}
}
?>
</body>
</html>
[/code]
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd ">
<html lang='en'>
<head>
<META HTTP-EQUIV='Content-Type' CONTENT='text/html; charset=ISO-8859-1'>
<title>Page title</title>
<style type="text/css">
table {
border-collapse: collapse;
border: solid 1px black;
}
td {
padding: 2px 6px;
border: solid 1px black;
}
</style>
</head>
<body>
<form action='<?php echo $_SERVER['PHP_SELF']; ?>' method='post'>
<p>Search for: <input type='text' name='search' size='20' maxlength='64'></p>
<p><input type='submit' value='Search'></p>
</form>
<?php
if(isset($_POST['search']))
{
$connx = mysql_connect('localhost', '*****', '*****') or die("connx");
$db = mysql_select_db('test') or die(mysql_error());# convert to upper case, trim it, and replace spaces with "|":
$search = (ini_get('magic_quotes_gpc')) ? stripslashes($_POST['search']) :
$_POST['search'];
$search = mysql_real_escape_string($search);
$search = strtoupper(preg_replace('/s+/', '|', trim($_POST['search'])));# create a MySQL REGEXP for the search:
$regexp = "REGEXP '[[:<:]]($search) [[:>:]]'";
$query = "SELECT * FROMWHERE UPPER( users
) $regexp OR ". description
"$regexp"; name
$result = mysql_query($query) or die($query . " - " . mysql_error());
echo "<table>n";
while($row = mysql_fetch_assoc($result))
{
echo "<tr>";
foreach($row as $key => $value)
{
echo "<td>$value</td>";
}
echo "</tr>n";
}
}
?>
</body>
</html> [/QUOTE]
echo "<td>$value</td>";
}
[/QUOTE]
[code=php]...[/code]
[/noparse] tags (instead of "quote" tags), it will be much easier for us to read.[code=php]
echo "<table>n";
while($row = mysql_fetch_assoc($result))
{
echo "<tr>";
foreach($row as $key => $value)
{
echo "<td>$value</td>";
}
echo "</tr>n";
}
[/code]
[code=php]
foreach($row as $key => $value)
[/code]
[code=php]
echo "<table>n";
while($row = mysql_fetch_assoc($result))
{
echo "<tr>";
echo "<td>{$row['some_field']}</td>";
echo "<td>{$row['some_other_field']}</td>";
echo "</tr>n";
}
[/code]
[code=php]
# create a MySQL REGEXP for the search:
$regexp = "REGEXP '[[:<:]]($search)[[:>:]]'";
$query = "SELECT * FROM users
WHERE UPPER(description
) $regexp OR ".
"name
$regexp";
[/code]
[code=php]
<?php
if(isset($_POST['search']))
{
$connx = mysql_connect('localhost', 'cms_main', ',J4(tgM2X.(~') or die("connx");
$db = mysql_select_db('cms_main') or die(mysql_error());
# convert to upper case, trim it, and replace spaces with "|":
$search = (ini_get('magic_quotes_gpc')) ? stripslashes($_POST['search']) :
$_POST['search'];
$search = mysql_real_escape_string($search);
$search = strtoupper(preg_replace('/s+/', '|', trim($_POST['search'])));
# create a MySQL REGEXP for the search:
$regexp = "REGEXP '[[:<:]]($search)[[:>:]]'";
$query = "SELECT * FROM galleries
WHERE UPPER(keywords
) $regexp OR ".
"name
$regexp";
$result = mysql_query($query) or die($query . " - " . mysql_error());
mysql_num_rows() in an if() statement to test for a result of 0.
echo "<table>n";
while($row = mysql_fetch_assoc($result))
{
echo "<tr>";
echo "<td><img src=../thumbs/photos/{$row['folder']}/{$row['date']}-{$row['num']}/{$row['thumbimage']} border=1></td>";
echo "<td>{$row['name']}</td>";
echo "<td>{$row['date']}</td>";
echo "<td><a href=../members/viewgallery.php?id={$row['id']} target=blank>View Photoset</a></td>";
echo "</tr>n";
}
}
?>
[/code]
[code=php]
mysql_num_rows() in an if() statement to test for a result of 0.
[/code]
[code=php]
<form action='<?php echo $_SERVER['PHP_SELF']; ?>' method='post'>
<p>Search for: <input type='text' name='search' size='20' maxlength='64'></p>
<p><input type='submit' value='Search'></p>
</form>
<?php
if(isset($_POST['search']))
{
$connx = mysql_connect('localhost', 'xxxxxxx', 'xxxxxxx') or die("connx");
$db = mysql_select_db('xxxxxxx') or die(mysql_error());
# convert to upper case, trim it, and replace spaces with "|":
$search = (ini_get('magic_quotes_gpc')) ? stripslashes($_POST['search']) :
$_POST['search'];
$search = mysql_real_escape_string($search);
$search = strtoupper(preg_replace('/s+/', '|', trim($_POST['search'])));
# create a MySQL REGEXP for the search:
$regexp = "REGEXP '[[:<:]]($search)[[:>:]]'";
$query = "SELECT * FROM xxxxxxx
WHERE UPPER(xxxxxxx
) $regexp OR ".
"xxxxxxx
$regexp";
$result = mysql_query($query) or die($query . " - " . mysql_error());
echo "<table>n";
while($row = mysql_fetch_assoc($result))
{
echo "<tr>";
echo "<td><img src=../thumbs/{$row['type']}/{$row['folder']}/{$row['date']}-{$row['num']}/{$row['thumbimage']} border=1></td>";
echo "<td>{$row['name']}</td>";
echo "<td>{$row['date']}</td>";
echo "<td><a href=view.php?id={$row['id']} target=blank>View</a></td>";
echo "</tr>n";
}
}
?>
[/code]
[code=php]
$result = mysql_query($sql);
if(mysql_num_rows($result) > 0) {
// do the stuff you want to do when results are found here
}
else {
// no results were found, so handle that situation here, for example:
echo "<p>Sorry, no results matched your search.</p>";
}
[/code]
[code=php]
# create a MySQL REGEXP for the search:
$regexp = "REGEXP '[[:<:]]($search)[[:>:]]'";
$query = "SELECT * FROM galleries
WHERE UPPER(keywords1
) $regexp OR ".
"keywords2
$regexp";
$result = mysql_query($sql);
echo "<table>n";
if(mysql_num_rows($result) > 0)
{
echo "<tr>";
echo "<td><img src=../thumbs/{$row['type']}/{$row['folder']}/{$row['date']}-{$row['num']}/{$row['thumbimage']} border=1></td>";
echo "<td>{$row['name']}</td>";
echo "<td>{$row['date']}</td>";
echo "<td><a href=../members/viewgallery.php?id={$row['id']} target=blank>View</a></td>";
echo "</tr>n";
}
}
else {
echo "<p>Sorry, no results matched your search.</p>";
}
[/code]
[code=php]<?php
if(isset($_POST['search']))
{
$connx = mysql_connect('localhost', 'cms_main', ',J4(tgM2X.(~') or die("connx");
$db = mysql_select_db('cms_main') or die(mysql_error());
# convert to upper case, trim it, and replace spaces with "|":
$search = (ini_get('magic_quotes_gpc')) ? stripslashes($_POST['search']) :
$_POST['search'];
$search = mysql_real_escape_string($search);
$search = strtoupper(preg_replace('/s+/', '|', trim($_POST['search'])));
# create a MySQL REGEXP for the search:
$regexp = "REGEXP '[[:<:]]($search)[[:>:]]'";
$query = "SELECT * FROM galleries
WHERE UPPER(keywords57
) $regexp OR ".
"keywords58
$regexp OR ".
"keywords59
$regexp OR ".
"keywords60
$regexp";
$result = mysql_query($query) or die($query . " - " . mysql_error());
echo "<table>n";
while($row = mysql_fetch_assoc($result))
{
echo "<tr>";
echo "<td><img src=../thumbs/{$row['type']}/{$row['folder']}/{$row['date']}-{$row['num']}/{$row['thumbimage']} border=1></td>";
echo "<td>{$row['name']}</td>";
echo "<td>{$row['date']}</td>";
echo "<td><a href=../members/viewgallery.php?id={$row['id']} target=blank>View Photoset</a></td>";
echo "</tr>n";
}
}
else {
echo "<p>Sorry, no results matched your search.</p>";
}
?> [/code]
0.1.9 — BETA 5.18