Page 1 of 1
Database search
Posted: Wed Jun 27, 2007 9:08 am
by seth_web
I am trying to search three columns of a database.
Below is the code I was thinking that the code I needed.
I get everything from the database when I search, and I get the same results no matter what I search.
Code: Select all
include_once ("include/db_connect.php") ;
$var = @$_GET['q'] ;
$trimmed = trim($var);
$query = "SELECT * FROM mountzion WHERE author LIKE \"%$trimmed%\"
OR title LIKE \"%$trimmed%\"
OR topic LIKE \"%$trimmed%\" ";
$result =mysql_query($query);
while ($rere = mysql_fetch_array($result, MYSQL_NUM)) {
echo $rere[2]; echo "<br>";
echo $rere[3];echo "<br>";echo "<br>";
}
Posted: Wed Jun 27, 2007 9:15 am
by volka
Interesting.
And... do you have a question?
Btw, did you add the @ because there was an annoying warning message?
Posted: Wed Jun 27, 2007 9:57 am
by pickle
Echo the query to see what $trimmed gets assigned as.
Posted: Wed Jun 27, 2007 11:12 am
by ReverendDexter
If you're only searching three fields, why are you doing a "SELECT *"?
And, what was your question, exactly?
Posted: Wed Jun 27, 2007 12:55 pm
by seth_web
Pickle,
I tried your idea and this is what I got :
SELECT * FROM mountzion WHERE author LIKE "%spurgeon%" OR title LIKE "%spurgeon%" OR topic LIKE "%spurgeon%"
Does that look right?
ReverendDexter,
I do not know of any other way of getting data besides SELECT.
I am trying to figure out why I am not getting any data.
Posted: Wed Jun 27, 2007 12:59 pm
by pickle
That's a perfectly fine query. If you're getting ALL rows returned when you run that query, then every row in your table has 'spurgeon' in either the `author`, `title`, or `topic` field - there's no other explaination.
As for getting the same results every time... it could be a couple of things. Either your query is the same each time regardless of what you search for, or each row in the table is identical & always contain a word you're searching for. Try searching for something completely junk (like ' asdfasd789459a8d' ) & see if it returns anything.
Posted: Wed Jun 27, 2007 1:03 pm
by seth_web
Now everything is blank!
Posted: Wed Jun 27, 2007 1:09 pm
by pickle
Ok, that's fine - that just means that there are no rows that match your search string - so the problem of getting the same results no matter what you search has either gone away or never existed. Your other problem of getting everything from the database when you search is also not always happening.
So, we're going to need some more detail on exactly when the problem happens & what you've tried.
Posted: Wed Jun 27, 2007 1:13 pm
by seth_web
I think that the problem is happening in the displaying.
If the query is fine, it makes sense that it would be the output.
But what the problem is, I do not know.
Posted: Wed Jun 27, 2007 2:09 pm
by Benjamin
volka wrote:Btw, did you add the @ because there was an annoying warning message?
Posted: Wed Jun 27, 2007 5:49 pm
by ReverendDexter
seth_web wrote:
I do not know of any other way of getting data besides SELECT.
I am trying to figure out why I am not getting any data.
Sorry, that was supposed to be "Why "SELECT
*", as opposed to a "SELECT field1, field2 field3"?"
What I was wondering is why you were selecting everything if you were only interested in three fields.
Posted: Wed Jun 27, 2007 6:36 pm
by seth_web
I am only searching three fields, but I am out putting more data than the three.
Does this cause any problems?
Posted: Wed Jun 27, 2007 6:43 pm
by AKA Panama Jack
Change your query to...
Code: Select all
$query = "SELECT * FROM mountzion WHERE author LIKE '%" . $trimmed . "%'
OR title LIKE '%" . $trimmed . "%'
OR topic LIKE '%" . $trimmed . "%' ";
Don't use double quotes around the items you are searching for.