Log in

View Full Version : Resolved search through mysql for info



auriaks
02-16-2010, 01:26 AM
Hi,

is it possible to search for mysql database with PHP??

example:

In input I insert word: "game".
And it will show me all rows id that word is in the name.
5 rows in table with values: Funny games, nice pool games, ricky marton, good night, funny lotus.

So, the result would be:
Funny games and nice pool games.
If I insert "funny", result: Funny games, funny lotus.

Nile
02-16-2010, 01:41 AM
Use mysql LIKE function.


$query = mysql_query("SELECT field FROM table WHERE field LIKE '%{$search}%'") or die(mysql_error());

auriaks
02-16-2010, 02:03 AM
I think I have problems with it :D



<?php
if (isset($_POST['searcher'])) {
include $_SERVER['DOCUMENT_ROOT'] . '/content/processes/db_conn.php';
$search = $_POST['search'];
$query = mysql_query("SELECT field FROM darbai WHERE field LIKE '%{$search}%'") or die(mysql_error());
while ($query) {
echo "$search<br/>";
}}
?>
<form action='search.php' method='post' enctype='multipart/form-data'>
<table align='center'>
<tr>
<td align='right'><td align='right'><font size='1' color='white' face='Verdana'><b><small>Write a word!</small></b></font></td>
<td><textarea name='search' MAXLENGTH=200 cols='20' rows='1' ></textarea></td>
</tr>
<tr align='right'>
<td align='right'><td align='right'></td>
<td align='left'>
<input type='submit' name='searcher' value=' Search '>
<input type='reset' value=' Trinti '>
</td>
</tr>
</table>
</form>


What is wrong?? Because I got errors...

Nile
02-16-2010, 02:09 AM
It's a normal query... treat it like one... 'field' Should be the name of the field your searching for. and while(query) should have some sort of mysql_fetch_....

auriaks
02-16-2010, 02:14 AM
Can you explain more about the field?? What should be there?

Nile
02-16-2010, 02:23 AM
Its just liek saying:


SELECT `name` FROM `table` WHERE `name` = 'BOB'

Or you can do:


SELECT * FROM darbai WHERE field LIKE '%{$search}%'

auriaks
02-16-2010, 02:32 AM
I'm really sorry, but still didn't understood it...

I want to search in table: darbai
for value from form (eg: work)
it needs rows or smth else?

Nile
02-16-2010, 02:37 AM
Give me your table structure.

auriaks
02-16-2010, 02:50 AM
CREATE TABLE IF NOT EXISTS `darbai` (
`id` int(11) NOT NULL auto_increment,
`name` varchar(300) character set utf8 collate utf8_lithuanian_ci NOT NULL,
`type` varchar(20) character set utf8 collate utf8_lithuanian_ci NOT NULL,
`nick` varchar(30) character set utf8 collate utf8_lithuanian_ci NOT NULL,
`format` varchar(100) character set utf8 collate utf8_lithuanian_ci NOT NULL,
`filename` varchar(100) character set utf8 collate utf8_lithuanian_ci NOT NULL,
`date` date NOT NULL,
`time` time NOT NULL,
`about` varchar(1000) character set utf8 collate utf8_lithuanian_ci NOT NULL,
`userIP` varchar(30) character set utf8 collate utf8_lithuanian_ci NOT NULL,
`identity` int(5) NOT NULL,
`rodyti` varchar(30) character set utf8 collate utf8_lithuanian_ci NOT NULL,
`URL` varchar(2000) character set utf8 collate utf8_lithuanian_ci NOT NULL,
`all_rank` int(100) NOT NULL,
`good_rank` int(100) NOT NULL,
`broken` varchar(20) character set utf8 collate utf8_lithuanian_ci NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=22 ;

Nile
02-16-2010, 02:54 AM
SELECT * FROM darbai WHERE name LIKE '%{$search}%'

auriaks
02-16-2010, 03:08 AM
It's done! Thanks :) I was looking for that almost 2 months... Appreciate that :D

Nile
02-16-2010, 03:16 AM
Lol... even with out that you could've used strpos.

auriaks
02-16-2010, 03:21 AM
its more complex to me :)