Page 1 of 2

How to Search trough a database

Posted: Tue May 23, 2006 1:45 pm
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!

Posted: Tue May 23, 2006 1:52 pm
by bdlang
This is really more an SQL question; what version of which RDBMS are you working with?

how to check it

Posted: Tue May 23, 2006 1:55 pm
by Milan
How can i check the exact version?

MySQL server is ver 5.0 and PHP 4

Posted: Tue May 23, 2006 2:09 pm
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 :)

YES

Posted: Tue May 23, 2006 2:24 pm
by Milan
THAT'S EXACTLY WHAT I NEEDED !

thanks!

Posted: Tue May 23, 2006 3:20 pm
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"

Posted: Tue May 23, 2006 3:24 pm
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']

Posted: Tue May 23, 2006 3:27 pm
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
}
}

Posted: Tue May 23, 2006 3:29 pm
by Milan
i will try it right away!

Posted: Tue May 23, 2006 3:45 pm
by Flamie
found a little bug in my code, use !== false instead of != false :)

hmmm

Posted: Tue May 23, 2006 3:48 pm
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

Posted: Tue May 23, 2006 3:50 pm
by Flamie
try strpos() isntead, stripos is a PHP5 function you might not have it.

Posted: Tue May 23, 2006 3:55 pm
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!

Posted: Tue May 23, 2006 3:56 pm
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.

yes

Posted: Tue May 23, 2006 4:01 pm
by Milan
that's it!

thanks! you have realy tought me a lot today!

Milan