Forum Moderators: coopster

Message Too Old, No Replies

Please Help! Search MySQL using PHP

         

Iseman

11:07 pm on Aug 12, 2004 (gmt 0)

10+ Year Member



I have created a simple PHP/MySQL site described below and it prints out (on screen) ALL 200,000 records from the database "aad". I know it's a tall order but from an expert (unlike me), it should be pretty straighforward. I would greatly appreciate somebody's help in enabling me to SEARCH the database according to (1) 'bn' (a six digit number) AND (2) 'dt' (date field) range (to and from) such that when a user enters the buno (e.g., 165655) and a date range (10-08-2003 thru 01-15-2004) from a form, it will return a "downloadable" LIST of all records of buno 165655 from within the date range specified... THANKS IN ADVANCE!

<html>
<head>
<title>AME AAD FILES</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>
<h2 align="center">First AME Database Testing Using PHP and MySQL</h2
>

<table align="center" border="1">
<tr>
<th>#</th>
<th>BUNO</th>
<th>DATE</th>
<th>Hour-Minute</th>
<th>DOWNLOAD</th>
</tr>

<?php
# Bring in db connection code
include("aad_db.php");

# Define SQL query
$q = "SELECT * FROM aad";

# Send query to MySQL and store results in buffer named $r
if($r=mysql_query($q,$db))
{
# Loop through result set
while ($d=mysql_fetch_assoc($r))
{
# Print out each dynamic HTML row, NOTE: Files must be in the proper folder
printf("<tr>\n <td>%s</td>\n", $d['num']);
printf("<td>%s</td>\n", $d['bn']);
printf("<td>%s</td>\n", $d['dt']);
printf("<td>%s</td>\n", $d['hhmm']);
printf("<td><a href=E:/AME/%s>download</a></td>\n</tr>\n", $d['aad']);
}
}
?>
</table>
</body>
</html>

Timotheos

11:48 pm on Aug 12, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi Iseman... Welcome to WebmasterWorld!

What you want to use is the WHERE clause [dev.mysql.com]

$q = "SELECT * FROM aad WHERE bn=165655 AND (dt > '2003-10-08' AND dt < '2004-01-15')";

The year usually comes first in the date field.

Hope that makes sense,
Tim

Iseman

11:55 pm on Aug 12, 2004 (gmt 0)

10+ Year Member



Thanks Tim! The Webmaster World is the BEST! Thanks for your response (that was quick!). My next question is, how do we create a FORM where the user can enter the buno AND the date range and it will LIST the result(s) for him/her on a web page? Thank you so much!

Sincerely,
Roger

Timotheos

3:35 pm on Aug 13, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi Iseman,

Take some time understanding this thread on the basics of extracting data for mysql [webmasterworld.com]. It shows you how to make a form and build a query from it. Let me know if you run into any roadblocks.

Tim

Iseman

5:20 pm on Aug 13, 2004 (gmt 0)

10+ Year Member



Thanks Tim! I already started reading the thread and will start coding. I appreciate your help and will definitely come back for more guidance. Thanks!

Roger

Iseman

8:25 pm on Aug 16, 2004 (gmt 0)

10+ Year Member



Hello Again PHP experts, php rookie here with a follow up request for assistance. With the help of examples, I was able to generate a "search" code included below. Please help me define an additional search criteria that would enable the user to choose a "date range" for a specific Bureau Number. As it is right now, the search asks user to enter a Bureau Number and one Bureau Number can have up to 1,000 returns. How do I insert another "date range" (to: from: ) criteria into the code? Your help is MUCH APPRECIATED.

-Roger

php code is as follows:

<html>
<head>
<title>AAD BUNO SEARCH</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>
<form method="post" action="<?php print $_SERVER['../PHP_SELF'];?>">
Search for BUNO:
<input type="text" name="s" value="<?php print $_POST['s'];?>"><br>
<input type="submit" name="submit" value="Search">
</form>

<?php
# Create a local variable with form input
$s = $_POST['s'];

#Is there a value in $s?
if ($s)
{
print "<hr>\n";

# Connect to database
include("aad_db.php");

# Create an array of $words from search input string $s
# $words = split(" ", $s);
$words = split("[; ,.]+", $s);

# Define a query
$q = "SELECT * FROM aad WHERE ";

#work with each element of the array #words
foreach ($words as $w)
{
$q .= "(bn LIKE '%$w%')";
}
$r = mysql_query($q, $db) or die("Query Failed:<br>$q<br><br>" . mysql_error());
while ($bn = mysql_fetch_array($r, MYSQL_ASSOC))
{
extract ($bn);
print "<a href=myaadsingle.php?bn=$bn>";
print "$bn - STRIPID: ($aad)";
print "</a><br>\n";
}
}
?>
</body>
</html>

Timotheos

5:47 am on Aug 17, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi Iseman,

Dates are such a pain ;-)

So probably the easiest thing to do is make some drop down boxes. You could get a lot fancier with some javascript. There's plenty of canned pop up calendars on the net to do this.

But back to your selects. One for the start month like so...

<select name="start_month" size="1">
<option value="1" SELECTED>January</option>
<option value="2">February</option>
<option value="3">March</option>
<option value="4">April</option>
<option value="5">May</option>
<option value="6">June</option>
<option value="7">July</option>
<option value="8">August</option>
<option value="9">September</option>
<option value="10">October</option>
<option value="11">November</option>
<option value="12">December</option>
</select>

Do something similar for the day and one for the year. The same for the end date.

Now your post data with have these variables which you can use in you sql statement.

$start_date = $_POST['start_year'] . "-" . $_POST['start_month'] . "-" . $_POST['start_day'];

same for $end_date

$q = "SELECT * FROM aad WHERE bn=165655 AND (dt > $start_date AND dt < $end_date)";

Well that's one way to do it.

Iseman

7:30 pm on Aug 17, 2004 (gmt 0)

10+ Year Member



I will certainly try that! Thanks you very much!

Iseman

9:28 pm on Aug 18, 2004 (gmt 0)

10+ Year Member



You were right about the dates being such a pain...

And before you made the drop-down list suggestion, I had this code working: Pls. excuse the way it was written; I'm still very new at this...

<snip>code dump</snip>

<form method="post" action="<?php print $_SERVER['../PHP_SELF'];?>">
<b>Enter Bureau Number:</b><input type="text" name="s1" value="<?php print $_POST['s1'];?>" size=10><br>
<b>Enter Date Range:</b><br>(yyyy-mm-dd)
from: <input type="text" name="s2" value="<?php print $_POST['s2'];?>" size=10>
to: <input type="text" name="s3" value="<?php print $_POST['s3'];?>" size=10>
<input name="Submit" type=Submit value="Search"></td></form>

<?php
$s1 = $_POST['s1'];
$s2 = $_POST['s2'];
$s3 = $_POST['s3'];

if (!$s1)
{
print "Please Enter a Bureau Number...";
}
elseif (strlen($s1)!== 6)
{
print "Sorry! $s1 is not a valid BUNO.";
}
else
{
if ($s1)
{
if ($s2)
{
if ($s3)
{
print "<hr>\n";
print "<b>Search Results</b> (click on ADD file to download)";
print "<br>\n";
print "<br>\n";
include("aad_db.php");

$word1 = split("[; ,.]+", $s1);
$word2 = split("[; ,.]+", $s2);
$word3 = split("[; ,.]+", $s3);

$q = "SELECT * FROM aad WHERE ";

foreach ($word1 as $w1)
{
foreach ($word2 as $w2)
{
foreach ($word3 as $w3)
{
$q .= "(bn LIKE '%$w1%') and (dt > '%$w2%' AND dt < '%$w3%' )";
}
}
}
if($r=mysql_query($q,$db))
{
while ($bn = mysql_fetch_array($r, MYSQL_ASSOC))
{
extract ($bn);
print "$bn - STRIPID: ";
print " <a href=I:/AME/$bn/2001/$aad>$aad</a>\n";
print "</a><br>\n";
}
}
}
}
}
}
?>
</body>
</html>

And it works! Well somewhat... I still have to create a "count" script where the results would tell the user how many files (if any) were found; and if none, to say, "No files found" (I need help on that as well...)

Anyway, when I created the dropdown list for the dates, it didn't produce the results as the code above... Could you pls. help in determining what's wrong and maybe refining the code as well (very crude)... sorry.

Here it is:

<snip>code dump</snip>

<?php
# Create a local variable with form input
$s1 = $_POST['s1'];
$s2 = $_POST['start_year'] . "-" . $_POST['start_month'] . "-" . $_POST['start_day'];
$s3 = $_POST['end_year'] . "-" . $_POST['end_month'] . "-" . $_POST['end_day'];

if (!$s1)
{
print "Please Enter a Bureau Number...";
}
elseif (strlen($s1)!== 6)
{
print "Sorry! $s1 is not a valid BUNO.";
}
else
{

if ($s1)
{
if ($s2){
if ($s3){

print "<hr>\n";
print "<b>Search Results</b> (click on ADD file to download)";
print "<br>\n";
print "<br>\n";

include("aad_db.php");

$word1 = split("[; ,.]+", $s1);
$word2 = split("[; ,.]+", $s2);
$word3 = split("[; ,.]+", $s3);

$q = "SELECT * FROM aad WHERE ";

foreach ($word1 as $w1)
{
foreach ($word2 as $w2){
foreach ($word3 as $w3){
#$q .= "(bn LIKE '%$w1%') and (dt > $s2 AND dt < $s3 )";
$q .= "(bn LIKE '%$w1%') and (dt > '%$w2%' AND dt < '%$w3%' )";
}
}
}
print "$s1, $s2, $s3<br>\n";
print "$w1, $w2, $w3";
if($r=mysql_query($q,$db))
{
while ($bn = mysql_fetch_array($r, MYSQL_ASSOC))
{
extract ($bn);
print "$bn - STRIPID: ";
print " <a href=I:/AME/$bn/2001/$aad>$aad</a>\n";
print "</a><br>\n";
}
}
}
}
}
}
?>

<form method="post" action="<?php print $_SERVER['../PHP_SELF'];?>">
<b>Enter Bureau Number:</b><input type="text" name="s1" value="<?php print $_POST['s1'];?>" size=10><br>
<b>Enter Date Range:</b>
<br>FROM <input type="hidden" name="s2" value="<?php print $_POST['s2'];?>" size=10>
<select name="start_month" size="1">
<option value="01" selected>January</option>
<option value="02">February</option>
<snip>code dump</snip></select>

<select name="start_day" size="1">
<option value="01" selected>01</option>
<snip>code dump</snip>
</select>

<select name="start_year" size="1">
<option value="1988">1988</option>
<snip>code dump</snip>
<option value="2003">2003</option>
<option value="2004" selected>2004</option>
</select>

<br>TO: <input type="hidden" name="s3" value="<?php print $_POST['s3'];?>" size=10>
<select name="end_month" size="1">
<option value="01">January</option>
<snip>code dump</snip>
<option value="12">December</option></select>

<select name="end_day" size="1">
<option value="01">01</option>
<option value="02">02</option>
<snip>code dump</snip>
</select>

<select name="end_year" size="1">
<option value="1988">1988</option>
<snip>code dump</snip>
<option value="2004" selected>2004</option>
</select>

<br><input type=Submit value="Search"></form>

THANKS IN ADVANCE!

[edited by: ergophobe at 10:50 pm (utc) on Aug. 18, 2004]
[edit reason] Code dump snipped [/edit]

Timotheos

10:53 pm on Aug 18, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Whoo boy. Ok, I'm not sure what you're doing here...
$word1 = split("[; ,.]+", $s1);
$word2 = split("[; ,.]+", $s2);
$word3 = split("[; ,.]+", $s3);

$q = "SELECT * FROM aad WHERE ";

foreach ($word1 as $w1)
{
foreach ($word2 as $w2){
foreach ($word3 as $w3){
#$q .= "(bn LIKE '%$w1%') and (dt > $s2 AND dt < $s3 )";
$q .= "(bn LIKE '%$w1%') and (dt > '%$w2%' AND dt < '%$w3%' )";
}
}
}

I'd just get rid of all the word variables and the foreach loops. Let's take it back to the line you commented out and put some single quotes in around $s2 and $s3.

$q .= "(bn LIKE '%$w1%') and (dt > '$s2' AND dt < '$s3')";

I'm assuming here your dt field is of type 'date'.

Another debugging trick is to print your sql statement and make sure it looks like what you want.

Iseman

6:08 pm on Aug 19, 2004 (gmt 0)

10+ Year Member



I knew I was creating a monster; and thank you for not being too harsh on me. I wasn't sure which lines I can safely remove. In my previous msg, the 1st set of code already works, but the user had to input the dates himself/herself. I took your advice and removed the 'word' variables and put single quotes as such:

$q .= "(bn LIKE '%$w1%') and (dt > '$s2' AND dt < '$s3')";

but it didn't return any results...

and yes, dt is a date field...

any further assistance is much appreciated. Sorry for the mess.

Timotheos

6:48 pm on Aug 19, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Well after this line put
echo $q;
and let's see what your sql statement ends up looking like.

Iseman

7:25 pm on Aug 19, 2004 (gmt 0)

10+ Year Member



echo $q returned:

SELECT * FROM aad WHERE (bn LIKE '%162888%') and (dt > '2004-01-01' AND dt < '2004-03-01' )

which is what I entered in the list box...

still it won't execute these lines that worked in the previous code:

if($r=mysql_query($q,$db))
{
while ($bn = mysql_fetch_array($r, MYSQL_ASSOC))
{
extract ($bn);
print "$bn - STRIPID: ";
print " <a href=I:/AME/$bn/2001/$aad>$aad</a>\n";
print "</a><br>\n";
}
}

Thanks again!

Timotheos

8:53 am on Aug 20, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Well I don't know what to tell you Iseman. You could try hard coding it to try and get the date part working.

$q = "SELECT * FROM aad WHERE dt > '2004-01-01'";

$r=mysql_query($q);

while ($bn = mysql_fetch_array($r))
{
print_r($bn);
}