How to Search trough a database

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Milan
Forum Commoner
Posts: 97
Joined: Wed May 17, 2006 6:08 pm

How to Search trough a database

Post by Milan »

I have a search form and action is pointing to the search.php

How can i search the field "topic name" and/or "topic descrption" ?
Both fields are text type.

thanks!
bdlang
Forum Contributor
Posts: 395
Joined: Tue May 16, 2006 8:46 pm
Location: Ventura, CA US

Post by bdlang »

This is really more an SQL question; what version of which RDBMS are you working with?
Milan
Forum Commoner
Posts: 97
Joined: Wed May 17, 2006 6:08 pm

how to check it

Post by Milan »

How can i check the exact version?

MySQL server is ver 5.0 and PHP 4
Flamie
Forum Contributor
Posts: 166
Joined: Mon Mar 01, 2004 3:19 pm

Post by Flamie »

mysql_query("SELECT topicname, topicdescription FROM <yourtablename> WHERE <put your conditions here>");

So for exemple if you're looking for all the topics post lets say by MrX youd go:
$result = mysql_query("SELECT topicname, topicdescription FROM <yourtablename> WHERE poster='MrX'");

Hope that helps :)
Milan
Forum Commoner
Posts: 97
Joined: Wed May 17, 2006 6:08 pm

YES

Post by Milan »

THAT'S EXACTLY WHAT I NEEDED !

thanks!
Milan
Forum Commoner
Posts: 97
Joined: Wed May 17, 2006 6:08 pm

Post by Milan »

So for exemple if you're looking for all the topics post lets say by MrX youd go:
$result = mysql_query("SELECT topicname, topicdescription FROM <yourtablename> WHERE poster='MrX'");
But what if i would like to see all fields where part of text or description is for example "video game"
Flamie
Forum Contributor
Posts: 166
Joined: Mon Mar 01, 2004 3:19 pm

Post by Flamie »

$result = mysql_query("SELECT * FROM <tablename> WHERE <conditions>");
then if you do
$row = mysql_fetch_object($result);
you'll have the data of all the fields in your "object" so if your table has like the fields (id, title, description)
you can access them like this:
$row->id
$row->title
$row->description

or if you prefer using an array instead of an object:
$row = mysql_fetch_array($result);
then you have:
$row['id']
$row['title']
$row['description']
Flamie
Forum Contributor
Posts: 166
Joined: Mon Mar 01, 2004 3:19 pm

Post by Flamie »

ah wait I missunderstood you!
Well theres no easy way to do it AFAIK, so you should try this:
$result = mysql_query("SELECT * FROM <tablename>");
$num = mysql_num_rows($result);
for($i=0;$i<$num;$i++)
{
$row = mysql_fetch_object($result);
if(stripos($row->description, "video game") != false) //that means video game is in the description
{
//do whatever you want here
}
}
Milan
Forum Commoner
Posts: 97
Joined: Wed May 17, 2006 6:08 pm

Post by Milan »

i will try it right away!
Flamie
Forum Contributor
Posts: 166
Joined: Mon Mar 01, 2004 3:19 pm

Post by Flamie »

found a little bug in my code, use !== false instead of != false :)
Milan
Forum Commoner
Posts: 97
Joined: Wed May 17, 2006 6:08 pm

hmmm

Post by Milan »

it says Fatal error: Call to undefined function: stripos() in c:\program files\apache group\apache\htdocs\project\search.php on line 16
Flamie
Forum Contributor
Posts: 166
Joined: Mon Mar 01, 2004 3:19 pm

Post by Flamie »

try strpos() isntead, stripos is a PHP5 function you might not have it.
Milan
Forum Commoner
Posts: 97
Joined: Wed May 17, 2006 6:08 pm

Post by Milan »

yup! works!

when i echo the $result i get Resource id #5

I know i am pain in the ass :) but how can i echo the "post name" field if the "video game" is found ? ( i want to link to it)

thanks a lot!
I owe you one!
Flamie
Forum Contributor
Posts: 166
Joined: Mon Mar 01, 2004 3:19 pm

Post by Flamie »

$row is the variable that contains the field data!!
so if your field is postname, inside the if you do this:
echo $row->postname."<br>";
edit:
And if you want to link to it:
echo "<a href='link to the post'>".$row->postname."</a><br>";

Hope that helps.
Milan
Forum Commoner
Posts: 97
Joined: Wed May 17, 2006 6:08 pm

yes

Post by Milan »

that's it!

thanks! you have realy tought me a lot today!

Milan
Post Reply