Database search

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
seth_web
Forum Newbie
Posts: 20
Joined: Wed Jun 06, 2007 10:00 am

Database search

Post 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>";

}
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

Interesting.
And... do you have a question?

Btw, did you add the @ because there was an annoying warning message?
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post by pickle »

Echo the query to see what $trimmed gets assigned as.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
ReverendDexter
Forum Contributor
Posts: 193
Joined: Tue May 29, 2007 1:26 pm
Location: Chico, CA

Post by ReverendDexter »

If you're only searching three fields, why are you doing a "SELECT *"?

And, what was your question, exactly?
seth_web
Forum Newbie
Posts: 20
Joined: Wed Jun 06, 2007 10:00 am

Post 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.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post 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.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
seth_web
Forum Newbie
Posts: 20
Joined: Wed Jun 06, 2007 10:00 am

Post by seth_web »

Now everything is blank!
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post 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.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
seth_web
Forum Newbie
Posts: 20
Joined: Wed Jun 06, 2007 10:00 am

Post 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.
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post by Benjamin »

volka wrote:Btw, did you add the @ because there was an annoying warning message?
User avatar
ReverendDexter
Forum Contributor
Posts: 193
Joined: Tue May 29, 2007 1:26 pm
Location: Chico, CA

Post 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.
seth_web
Forum Newbie
Posts: 20
Joined: Wed Jun 06, 2007 10:00 am

Post by seth_web »

I am only searching three fields, but I am out putting more data than the three.
Does this cause any problems?
User avatar
AKA Panama Jack
Forum Regular
Posts: 878
Joined: Mon Nov 14, 2005 4:21 pm

Post 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.
Post Reply