Page 2 of 2
Posted: Wed May 26, 2004 8:52 pm
by Joe
I think you must have either a code or mysql problem. Are you sure that you have set your db up correctly. Sometimes silly things can lead to later disasters. I'll look around for your error and post anything I find.
Regards
Posted: Wed May 26, 2004 8:54 pm
by feyd
Code: Select all
while($row = mysql_fetch_assoc($result))
{
print_r($row); // do whatever printing....
}
Posted: Wed May 26, 2004 8:55 pm
by Joe
Also just to note. You can use mysql_error() to see the exact error! If there is any that is!
Posted: Wed May 26, 2004 9:08 pm
by McGruff
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
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.
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.
Posted: Wed May 26, 2004 10:23 pm
by mikewooten
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
Posted: Thu May 27, 2004 12:10 am
by feyd
you are echoing $result, which contains the string "Resource id #3".. this comes from mysql.. replace that with the while loop.
Posted: Thu May 27, 2004 1:13 pm
by McGruff
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?
For example:
"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.
Posted: Thu May 27, 2004 3:19 pm
by mikewooten
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
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]";
}
?>
Posted: Thu May 27, 2004 4:41 pm
by McGruff
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):
Code: Select all
<?php
echo '<pre>';
print_r($_POST);
echo '</pre>';
?>
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:
Code: Select all
<?php
if(mysql_num_rows($result) == 0)
{
// no results case
} else {
// matches found
}
?>
?
Posted: Thu May 27, 2004 8:43 pm
by mikewooten
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
?>
?
Posted: Fri May 28, 2004 12:13 pm
by mikewooten
?
Posted: Fri May 28, 2004 2:49 pm
by McGruff
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.