Here is the full code that creates the query:
$query = "SELECT id, DATE_FORMAT(pubdate, '%Y %m-%d'), byline, headline, bodytext FROM stories ";
$where = "";
$sep = "WHERE ";
if ($strKeyword != "") {
$where .= $sep . "bodytext LIKE '" . $strKeyword . "' ";
$sep = "AND ";
}
if ($headline != "") {
$where .= $sep . "headline LIKE '" . $headline . "' ";
$sep = " AND ";
}
if ($bodytext != "") {
$where .= $sep . "bodytext LIKE '" . $bodytext . "' ";
$sep = " AND ";
}
if ($byline != "") {
$where .= $sep . "byline LIKE '" . $byline . "' ";
$sep = " AND ";
}
if ($section != "") {
$where .= $sep . "section LIKE '" . $section . "' ";
$sep = " AND ";
}
if ($page != "") {
$where .= $sep . "page LIKE '" . $page . "' ";
$sep = " AND ";
}
if ($keywords != "") {
$where .= $sep . "keywords LIKE '" . $keywords . "' ";
$sep = " AND ";
}
if ($series != "") {
$where .= $sep . "series LIKE '" . $series . "' ";
$sep = " AND ";
}
if ($caption != "") {
$where .= $sep . "caption LIKE '" . $caption . "' ";
$sep = " AND ";
}
if ($caption != "") {
$where .= $sep . "caption LIKE '" . $caption . "' ";
$sep = " AND ";
}
if ($strFrom != "" || $strTo != "") {
if ($strFrom != "" && $strTo == "") {
$where = $sep . "pubdate > '" . $strFrom . "'";
$sep = "AND";
}
else if ($strFrom == "" && $strTo != "") {
$where = $sep . "pubdate < '" . $strTo . "'";
$sep = "AND";
}
else if ($strFrom != "" && $strTo != "") {
$where = $sep . "pubdate BETWEEN '" . $strFrom . "' AND '" . $strTo . "'";
$sep = "AND";
}
}
$query .= $where;
$storyfile = tempnam ('/projects/sunarchive/htdocs/temp2', 'query');
$fp = fopen($storyfile,'w');
if($fp)
{
fwrite($fp,$query);
}
else
{
die('Could not open file.');
}
When I run a query using just the Date, it works, and outputs this Query string:
SELECT id, DATE_FORMAT(pubdate, '%Y %m-%d'), byline, headline, bodytext FROM stories WHERE pubdate BETWEEN '2002-07-01' AND '2002-08-30'
When I do a query using the Date and the Byline, it doesn't work, and outputs this Query string:
SELECT id, DATE_FORMAT(pubdate, '%Y %m-%d'), byline, headline, bodytext FROM stories AND pubdate BETWEEN '2002-07-01' AND '2002-08-30'
When I do a query using just the Byline and not the date, it outputs this query string:
SELECT id, DATE_FORMAT(pubdate, '%Y %m-%d'), byline, headline, bodytext FROM stories AND pubdate BETWEEN '1900-01-01' AND '2002-12-31'
Let me know if there is anything else you need. Thanks again for your help!