Domain Discussion Board

Go Back   Domain Discussion Board > General > Tutorials

 
Reply
 
LinkBack Thread Tools Display Modes
how to search in a Mysql-database
Old
  (#1 (permalink))
frih
Senior Member
frih is an unknown quantity at this point
 
frih's Avatar
 
Status: Offline
Posts: 194
Points: 536.4
Bank: 526.5
Total Points: 1,062.9
Donate
Join Date: Sep 2007
Rep Power: 0
how to search in a Mysql-database - 08-18-2008, 10:28 AM

[SIZE=2]Needed functions:
mysql_connect() (in this example it's presupposed that a connection to your database already exists)
explode()
array()
foreach()
implode()
mysql_query()
mysql_error()

php:
$keywords = 'searching in a mysql database';

You could also write a form where the keywords could be entered.

php:
$association = 'AND';

Search mode AND or OR, can also be defined e.g. by RADIO-buttons.

php:
$keywords = explode(" ",$keywords);

We must participate the keywords because we want later create a mysql query with them.

php:
$query = array();

foreach($keywords as $keyword) {
$query[] = "( `name_of_first_field` LIKE '%".$keyword."%'
OR `name_of_second_field` LIKE '%".$keyword."%'
OR `name_of_third_field` LIKE '%".$keyword."%'
OR `and_so_on` LIKE '%".$keyword."%')
";
}

Every keyword must be changed that we can use it in a mysql query. The %-symbol is a substitute symbol for the possible other text in the data row.

php:
$query = implode("\n ".$association." ",$query);

Now we have to put the keywords together again by using the AND or the OR-association.

php:
$sql = "
SELECT *
FROM `name_of_table`
WHERE ".$query."
;"

$res = mysql_query($sql);

if (!$res) {
/**
* This lines print the SQL statement in a viewable form.
*/
print '<pre>';
print $sql;
print '</pre>';
/**
* E_USER_ERROR will exit the script.
* E_USER_NOTICE and E_USER_WARNING won't stop the script.
*/
trigger_error('Database query failed: '.mysql_error(), E_USER_ERROR);
} else {
/**
* Add here the routines to print the results.
*/
}

...and at last, of course, we'll send the mysql - query and you could print the results as usual with mysql_fetch_object() or mysql_fetch_array()...

Here's the whole script:
php:
$keywords = 'searching in a mysql database';

$association = 'AND';

$keywords = explode(" ",$keywords);

$query = array();

foreach($keywords as $keyword) {
$query[] = "( `name_of_first_field` LIKE '%".$keyword."%'
OR `name_of_second_field` LIKE '%".$keyword."%'
OR `name_of_third_field` LIKE '%".$keyword."%'
OR `and_so_on` LIKE '%".$keyword."%')
";
}

$query = implode("\n ".$association." ",$query);

$sql = "
SELECT *
FROM `name_of_table`
WHERE ".$query."
;"

$res = mysql_query($sql);

if (!$res) {
/**
* This lines print the SQL statement in a viewable form.
*/
print '<pre>';
print $sql;
print '</pre>';
/**
* E_USER_ERROR will stop the script.
* E_USER_NOTICE und E_USER_WARNING won't stop the script.
*/
trigger_error('Database query failed: '.mysql_error(), E_USER_ERROR);
} else {
/**
* Add here the routines to print the results.
*/
}[/SIZE]



[SIZE=2]regards[/SIZE]
[SIZE=2]frih!![/SIZE]


Get Free Domain Now
visit-- http://www.geekquery.com/forum/index.php
   
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Points Per Thread View: 1.0
Points Per Thread: 15.0
Points Per Reply: 5.0


Similar Threads
Thread Thread Starter Forum Replies Last Post
Brandable Domain Names - LLLLL.com (Prounouncable and Clear) Zalaki Domain Names For Sale 0 07-20-2008 03:08 AM
Website and Application Content Databases Guest Other Marketing Techniques 4 06-06-2007 05:44 AM


Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 ©2007, Crawlability, Inc.