Page 1 of 1
Need a code for searching a a word in a mysql database
Posted: Wed Jun 16, 2010 8:59 am
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......
Re: Need a code for searching a a word in a mysql database
Posted: Wed Jun 16, 2010 9:09 am
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.
Re: Need a code for searching a a word in a mysql database
Posted: Wed Jun 16, 2010 9:44 am
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
Re: Need a code for searching a a word in a mysql database
Posted: Wed Jun 16, 2010 10:25 am
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

Re: Need a code for searching a a word in a mysql database
Posted: Thu Jun 17, 2010 12:37 am
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";
}
?>
Re: Need a code for searching a a word in a mysql database
Posted: Thu Jun 17, 2010 12:37 am
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