Pages: [1]
Author Topic: Searching a MySQL Database  (Read 2115 times)
0 Members and 1 Guest are viewing this topic.
Belthasar
Administrator
Level 10
*****
Posts: 1255



View Profile WWW
« on: June 20, 2007, 07:47:02 PM »

This is my first tutorial. I hope some of you find it useful.

In this tutorial you will make 3 pages:
config.php - contains database information
functions.php - contains search functions
search.php - the page that searches.
Please note that words after two slashes, //, are comments. Meant to help you understand the process.
config.php
Code:
<?php
//config.php
$host="localhost"
$user="db_user"//Database user
$password="db_pw"//DB User's Password
$conn mysql_connect($host,$user,$password)
   or die(
mysql_error());
mysql_select_db("d_base"$conn//d_base is the database name
   
or die(mysql_error());
?>

Now that you can connect to your database you will need functions that open it up and look through it. For this tutorial you will make 3 functions:
searchPhrase() - Returns results that contain an exact phrase
searchAny() - Returns results with any of the words in the phrase
searchAll() - Returns results with all the words in the phrase

Code:
<?php
//functions.php
function searchPhrase($term)
{
$sql "SELECT * FROM messages WHERE body LIKE '%$term%'"// Look into the table messages where the column body contains the search term.  
$result mysql_query($sql) or die(mysql_error());
while ($row mysql_fetch_assoc($result)) 
{
echo $row['subject'] , ' : ' $row['body'] , '<hr><br><br>'//Print out "subject : body"
}
}
?>


That is the simplest function. The next is a bit more complex.

Code:
<?php
function searchAll($term)
{
$search_array explode(" " $term); //Makes an array(basically a matrix) of the search term splitting it between spaces.
$size_search sizeof($search_array); //Gets the number of units in the array
for($i 0$i $size_search$i++) 
{
$search_array[$i] = "'%" $search_array[$i] . "%'"//Creates proper syntax for a sql statement
}
$search_term implode(" "$search_array); //Puts the search term back into a single string
$sql "SELECT * FROM smf_messages WHERE body LIKE $search_term";
$result mysql_query($sql) or die(mysql_error());
while ($row mysql_fetch_assoc($result)) 
{
echo $row['subject'] , ' : ' $row['body'] , '<hr><br><br>';
}
}
?>


Now the 3rd and most difficult function.

Code:
<?php
function searchAny($term)
{
$search_array explode(" " $term);
$size_search sizeof($search_array);
for($i 0$i $size_search$i++)
{
if($i == ($size_search 1))
$search_array[$i] = "'%" $search_array[$i] . "%'"//Prevents OR from being added to the last term
else
$search_array[$i] = "'%" $search_array[$i] . "%'" " OR"//Adds an OR to the end of each term.
}
$search_term implode(" "$search_array);
$sql "SELECT * FROM smf_messages WHERE body LIKE $search_term";
$result mysql_query($sql) or die(mysql_error());
while ($row mysql_fetch_assoc($result)) 
{
echo $row['subject'] , ' : ' $row['body'] , '<hr><br><BR><BR>';
}
}

?>


Now by this time I am sure you are wondering when you will actually be able to search for something.

Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>MySQL Search Page</title>
</head>
<body>
<p>
  <?Php
include('config.php'); //Gives this page access to the database.
include('functions.php'); //Gives this page access to the functions you just made.

if($_POST) //Makes sure to execute the code AFTER the form has been submitted.
{
switch($_POST['req']) //Depending on what the user selects, different functions will be executed.
{
case "All": echo searchAll($_POST['term']); break;
case "Any": echo searchAny($_POST['term']); break;
case "Phrase": echo searchPhrase($_POST['term']); break;
}
}

?>
</p>
<form id="form1" name="form1" method="post" action="search.php">
  <p>
    <input name="term" type="text" id="term" />
  </p>
  <p>
    <label>
    <input type="radio" name="req" value="All" />
All</label>
    <br />
    <label>
    <input name="req" type="radio" value="Any" checked="checked" />
Any</label>
    <br />
    <label>
    <input type="radio" name="req" value="Phrase" />
Phrase</label>
    <br />
    <br />
    <input type="submit" name="Submit" value="Submit" />
</p>
</form>
<p>&nbsp;</p>
</body>
</html>

Bet that didn't make much sense!
and thats it.... If you have any questions, I'll try my best to answer them....
Logged

@cechastain I just cut out your name tag. Hope to see you at the Holiday Inn tomorrow morning. I'm Jeremy Roberts
Follow me on twitter - http://twitter.com/Belthasar



Wyvern
General Mod
Level 10
*****
Posts: 1181



View Profile WWW
« Reply #1 on: June 21, 2007, 09:22:18 AM »

lol You're gonna confuse a lot of people here.

Anyway, maybe this will be alittle more useful to me when I understand a bit more with MySQL and PHP. I'm still learning.
Logged


Computer messed up. Inactive until I can do something...

Join The Nintegrity Boards!

Belthasar
Administrator
Level 10
*****
Posts: 1255



View Profile WWW
« Reply #2 on: June 21, 2007, 09:58:51 AM »

Yeah.....

I will put up an easier one to understand later. One that deals with time to change actions.

Likda like:

If the current time is greater than or equal to 8:00am Print: I am at work
If the current time is greater than or equal to 5:00pm Print: I am driving home
...
...
If the current time is greater than or equal to 12:00am Print: I am asleep...

Easier to understand. Besides time functions are really neat...
Logged

@cechastain I just cut out your name tag. Hope to see you at the Holiday Inn tomorrow morning. I'm Jeremy Roberts
Follow me on twitter - http://twitter.com/Belthasar

RaceProUK
Level 0
***
Posts: 58



View Profile WWW
« Reply #3 on: June 21, 2007, 12:05:34 PM »

This may not be MySQL (it's T-SQL), but it'll be easy to convert:
Code:
SELECT    CASE
                    WHEN CONVERT(nvarchar(5), GETDATE, 108) >= '17:00' THEN 'I am driving home'
                    WHEN CONVERT(nvarchar(5), GETDATE, 108) >= '08:00' THEN 'I am at work'
                    WHEN CONVERT(nvarchar(5), GETDATE, 108) >= '00:00' THEN 'I am asleep'
          END
Logged

RP Software

Belthasar
Administrator
Level 10
*****
Posts: 1255



View Profile WWW
« Reply #4 on: June 21, 2007, 12:35:51 PM »

I wasn't going to use a database for that. Just a switch statement and the date function
Logged

@cechastain I just cut out your name tag. Hope to see you at the Holiday Inn tomorrow morning. I'm Jeremy Roberts
Follow me on twitter - http://twitter.com/Belthasar

Wilhelm
Guest
« Reply #5 on: June 27, 2007, 05:37:47 PM »

Or you could just use a MySQL front-end like navicat lol.. Code does look SEXY tho  Kiss
Logged

Belthasar
Administrator
Level 10
*****
Posts: 1255



View Profile WWW
« Reply #6 on: June 27, 2007, 09:22:44 PM »

lol, is that like phpmyadmin?
Logged

@cechastain I just cut out your name tag. Hope to see you at the Holiday Inn tomorrow morning. I'm Jeremy Roberts
Follow me on twitter - http://twitter.com/Belthasar

Gori Fater
General Mod
Level 5 access member
*****
Posts: 589



View Profile WWW
« Reply #7 on: June 28, 2007, 05:56:05 AM »

Aha... So that's the way to link forms with PHP/MySQL. Thanks for the code. I'll try to use it to implement the possibility to search by arranger or game publisher on my sheet music database.
Logged

*****************************

http://www.gamemusicthemes.com

*****************************

Wilhelm
Guest
« Reply #8 on: July 02, 2007, 04:33:53 PM »

Navicat or other SQL frontends (the one i use i believe is simply called MySQL 5.0) makes it really easy.. you can still use all the text commands you would normally use but with the added bonus of having an organized GUI that helps you make sense of things, way easier to create tables and what not too..
Logged

Pages: [1]
Print
Jump to: