Need search within site code
Moderator: General Moderators
- feyd
- Neighborhood Spidermoddy
- Posts: 31559
- Joined: Mon Mar 29, 2004 3:24 pm
- Location: Bothell, Washington, USA
Code: Select all
while($row = mysql_fetch_assoc($result))
{
print_r($row); // do whatever printing....
}You might not necessarily want to use all of these. However, a good search engine should provide a wildcard so that you can specify suffixes or prefixes allowed/not allowed. You would have to parse the search terms for the wildcard, and then choose an appropriate clause.mikewooten wrote:what's RLIKE? how would i use it in the query that i already have? or would i use RLIKE instead of like?
if i were to use RLIKE in the query that i already have, where would i put it?
thanks
Or, a simple option is to go with one of always exact matches (no suffixes/prefixes) or always loose matches (suffixes & prefixes).
If $term == buff:
match buff, buffy, rebuffed:
"... LIKE '%" . $term . "%' ..."
match buff and rebuff:
"... RLIKE '.*" . $term . "[[:>:]]' ..."
match buff and buffy:
"... RLIKE '[[:<:]]" . $term . ".*' ..."
match buff only:
"... RLIKE '[[:<:]]" . $term . "[[:>:]]' ..."
It's all in the mysql manual which you can get from mysql.com.
Ideally, you would offer a full range of boolean options: phrase searches, and/or, +/-, >/< etc. MATCH rears it's ugly head at this point but I think it's better not to use it.
-
mikewooten
- Forum Contributor
- Posts: 169
- Joined: Wed Feb 11, 2004 12:13 pm
- Location: Duluth, Georgia
- Contact:
wher should i put the while($row = mysql_fetch_assoc($result))
{
print_r($row); // do whatever printing....
}
also i'm still gettting the error of Resource id #3, even when i add the mysql_error().
is the like and $term used in the sql? how would i use the like and $term in the sql statement? whats the variable $term used for, what variable is $term?
thanks
{
print_r($row); // do whatever printing....
}
also i'm still gettting the error of Resource id #3, even when i add the mysql_error().
is the like and $term used in the sql? how would i use the like and $term in the sql statement? whats the variable $term used for, what variable is $term?
thanks
For example:mikewooten wrote:is the like and $term used in the sql? how would i use the like and $term in the sql statement? whats the variable $term used for, what variable is $term?
"SELECT col1, col2, col3 FROM table WHERE col3 RLIKE '[[:<:]]" . $term . "[[:>:]]'"
$term is the search term.
You can search for multiple terms and in multipe cols by adding OR or AND clauses.
It would be worth checking out the mysql manual (mysql.com) for the syntax.
-
mikewooten
- Forum Contributor
- Posts: 169
- Joined: Wed Feb 11, 2004 12:13 pm
- Location: Duluth, Georgia
- Contact:
for $term thats used in the example above should it be set to something, should it have some value?
should the $term be set to something like
$term = "something";
or should it be
$term = "";
also when i use the while loop, there's no results when i view the page?
how can i get the results to display?
here's the code
should the $term be set to something like
$term = "something";
or should it be
$term = "";
also when i use the while loop, there's no results when i view the page?
how can i get the results to display?
here's the code
Code: Select all
<form action="<? $_SERVER['PHP_SELF']; ?>">
<font size="5" color="white"><strong>Search</strong></font>
<input type="text" name="search" size="13" maxlength="99">
<input type="submit" name="submit" value="SUBMIT" width="10" height="1">
</form>
<?php
$host = "localhost";
$user = "user";
$pass = "pass";
$tblname = "items";
$dbname = "db";
$item = $_GET['search'];
$link = mysql_connect("$host","$user","$pass") or die("Connection Error");
mysql_select_db($dbname) or die(mysql_error());
$query = "SELECT * FROM $tblname WHERE itemname = '$item'";
$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_row($result))
{
echo "$row[0]";
echo "$row[1]";
echo "$row[2]";
echo "$row[3]";
echo "$row[4]";
echo "$row[5]";
echo "$row[6]";
}
?>Yes - you do indeed need to declare $term first in the script.
If you are receiving search terms from a form, with method post, terms will be in the $_POST superglobal array.
This will help with debugging if you find you need to check exactly what has been submitted in $_POST (also works for other arrays such as the mysql fetch fns):
You could of course have multiple search terms such as: 'foo bar'. You can use explode() to get the individual values, then (dynamically) create an sql query.
If your query does not find any matches, there will be no rows to echo: mysql_num_rows will reveal all.
Script logic should check for this with something like:
If you are receiving search terms from a form, with method post, terms will be in the $_POST superglobal array.
This will help with debugging if you find you need to check exactly what has been submitted in $_POST (also works for other arrays such as the mysql fetch fns):
Code: Select all
<?php
echo '<pre>';
print_r($_POST);
echo '</pre>';
?>If your query does not find any matches, there will be no rows to echo: mysql_num_rows will reveal all.
Script logic should check for this with something like:
Code: Select all
<?php
if(mysql_num_rows($result) == 0)
{
// no results case
} else {
// matches found
}
?>
Last edited by McGruff on Mon Aug 08, 2005 9:55 pm, edited 1 time in total.
-
mikewooten
- Forum Contributor
- Posts: 169
- Joined: Wed Feb 11, 2004 12:13 pm
- Location: Duluth, Georgia
- Contact:
?
Code: Select all
what is $term declared as?
is it declared as $term = ""; ?
what goes in the quotes or declared as?
how would that script look if i created the sql dynamically?
would it be
<?php
$query = "SELECT * FROM $tblname WHERE LIKE '%'$item'%'";
?>
also, would i use the
<?
if(mysql_num_rows($result) == 0)
{
// no results case
} else {
// matches found
}
?>
instead of the
<?
while($row = mysql_fetch_row($result))
{
print_r($row);
printf ("ID: %s itemName: %s", $row[4], $row["itemName"]);
}
?>
or would i use the if statement after the while loop?
i saw the explode() on php.net, where in the code that i have would i use the explode()?
question about the explode, which would i use for this search that i'm using?
would I use this one?
<?
// Example 1
$pizza = "piece1 piece2 piece3 piece4 piece5 piece6";
$pieces = explode(" ", $pizza);
echo $pieces[0]; // piece1
echo $pieces[1]; // piece2
?>
or this one?
<?
// Example 2
$data = "foo:*:1023:1000::/home/foo:/bin/sh";
list($user, $pass, $uid, $gid, $gecos, $home, $shell) = explode(":", $data);
echo $user; // foo
echo $pass; // *
thanks
?>-
mikewooten
- Forum Contributor
- Posts: 169
- Joined: Wed Feb 11, 2004 12:13 pm
- Location: Duluth, Georgia
- Contact:
?
?
I think you need to spend some time reading the php manual and possibly a few basic tutorials. Stick at it and it will all start to make sense.
The mysql_num_rows() function, for example, takes a db result resource as an arg ($result in your script) and returns the number of rows in that resource. Once you've performed the query, use this to check if any results were found.
If num rows is 0, nothing matched the search: you'd display a "no matches" type of message to the user.
If num rows is 1 or more, you can loop through the result resource (the while(...) code), printing the results.
The mysql_num_rows() function, for example, takes a db result resource as an arg ($result in your script) and returns the number of rows in that resource. Once you've performed the query, use this to check if any results were found.
If num rows is 0, nothing matched the search: you'd display a "no matches" type of message to the user.
If num rows is 1 or more, you can loop through the result resource (the while(...) code), printing the results.