Need a code for searching a a word in a mysql 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

Post Reply
kishoremcp
Forum Newbie
Posts: 6
Joined: Tue Jun 16, 2009 1:42 am

Need a code for searching a a word in a mysql database

Post by kishoremcp »

hello every one
i am new to php
i have a database with name songs
the table name is sheet 9 and the columns in them are Movie Name, Singer, Release Year
Now i have approx 3000 entries in that table. Actually i exported this table from excel to mysql using a software.
so now i want to search a word in that database using php code.
i already did some google and found a script in html for initial search box page
now in that it is forwarding the page to the next page and in that i want the script to find the term which is given in my search box and should display the entire row in a table format which contains the specified search word

please help me in doing this......
aravona
Forum Contributor
Posts: 347
Joined: Sat Jun 13, 2009 3:59 pm
Location: England

Re: Need a code for searching a a word in a mysql database

Post by aravona »

From your searchbox, you need to put the action as a .php page ... say search.php (you can set the action to be self but this maybe easier for you as a beginner).

Then in this file you need to use php to post or get the search box info to a variable. I assume the one you have set up form your google code?

Then you need to do a mysql search based on the variable: $query = 'select * from table where name like '%$variable%' ' or something like that should do the job.

Then you just need to use the mysql functions to display this within an echo line. You will need to use something like mysql_fetch_assoc or whatever is best appropriate.

I hope thats enough to give you a start then you can at least post up some code when you have more of a problem.
kishoremcp
Forum Newbie
Posts: 6
Joined: Tue Jun 16, 2009 1:42 am

Re: Need a code for searching a a word in a mysql database

Post by kishoremcp »

Thanks for the help ....
but as of now i don't have such code....can u build me a script for me to do so......sorry for the trouble ....
I am not in to software actually so please guide me more
aravona
Forum Contributor
Posts: 347
Joined: Sat Jun 13, 2009 3:59 pm
Location: England

Re: Need a code for searching a a word in a mysql database

Post by aravona »

This is a help forum, not a please build it for me. There is a section for freelancers.

I suggest going to this site http://www.w3schools.com/ and giving it a go :)
kishoremcp
Forum Newbie
Posts: 6
Joined: Tue Jun 16, 2009 1:42 am

Re: Need a code for searching a a word in a mysql database

Post by kishoremcp »

<?php
/*set varibles from form */
$searchterm = $_POST['searchterm'];
trim ($searchterm);
/*check if search term was entered*/
if (!$searchterm){
echo 'Please enter a search term.';
}
/*add slashes to search term*/
if (!get_magic_quotes_gpc())
{
$searchterm = addslashes($searchterm);
}

/* connects to database */
@ $dbconn = new mysqli('localhost', 'root', 'root', 'songs');
if (mysqli_connect_errno())
{
echo 'Error: Could not connect to database. Please try again later.';
exit;
}
$query = 'SELECT Song Name, Singer, Movie Name, Release Year FROM sheet 9{resources}';
$result = db_query($query);
while ($row = db_fetch_object($result)) {
$Song Name="{$row->Song Name}";
$Singer="{$row->Singer}";
$Movie Name="{$row->Movie Name}";
$Release Year="{$row->Release Year}";
echo "<strong>".$org."</strong><br>\n";
if($Song Name!=""){ echo $Song Name."<br>\n"; }
if($Singer!=""){ echo $Singer."<br>\n"; }
if($Movie Name=""){ echo $Movie Name."<br>\n"; }
if($Release Year!=""){ echo $Release Year."<br>\n"; }
echo "<br>\n";
}
?>
kishoremcp
Forum Newbie
Posts: 6
Joined: Tue Jun 16, 2009 1:42 am

Re: Need a code for searching a a word in a mysql database

Post by kishoremcp »

<?php
/*set varibles from form */
$searchterm = $_POST['searchterm'];
trim ($searchterm);
/*check if search term was entered*/
if (!$searchterm){
echo 'Please enter a search term.';
}
/*add slashes to search term*/
if (!get_magic_quotes_gpc())
{
$searchterm = addslashes($searchterm);
}

/* connects to database */
@ $dbconn = new mysqli('localhost', 'root', 'root', 'songs');
if (mysqli_connect_errno())
{
echo 'Error: Could not connect to database. Please try again later.';
exit;
}
$query = 'SELECT Song Name, Singer, Movie Name, Release Year FROM sheet 9{resources}';
$result = db_query($query);
while ($row = db_fetch_object($result)) {
$Song Name="{$row->Song Name}";
$Singer="{$row->Singer}";
$Movie Name="{$row->Movie Name}";
$Release Year="{$row->Release Year}";
echo "<strong>".$org."</strong><br>\n";
if($Song Name!=""){ echo $Song Name."<br>\n"; }
if($Singer!=""){ echo $Singer."<br>\n"; }
if($Movie Name=""){ echo $Movie Name."<br>\n"; }
if($Release Year!=""){ echo $Release Year."<br>\n"; }
echo "<br>\n";
}
?>

Whats wrong in the code
i am not getting any result from mydatabase
Post Reply