tags:

views:

47

answers:

2
    $LDATE = '#' . $_REQUEST['LDateDay'] . '/' . $_REQUEST['LDateMonth'] . '/' . $_REQUEST['LDateYear'] . '#';
    $RDATE = '#' . $_REQUEST['RDateDay'] . '/' . $_REQUEST['RDateMonth'] . '/' . $_REQUEST['RDateYear'] . '#';
    include("../../sql.php");
    $myconn2 = mysql_connect(/*removed*/, $username, $password);
    mysql_select_db(/*removed*/, $myconn2);
    $LSQLRequest = "SELECT * FROM flight WHERE DepartureDate = ".$LDATE;
    $LFlights = mysql_query($LSQLRequest, $myconn2);
    $RSQLRequest = "SELECT * FROM flight WHERE DepartureDate = ".$RDATE;
    $RFlights = mysql_query($RSQLRequest, $myconn2);

Assuming that all the $_REQUESTs are valid numerical values for their appropriate fields in the day/month/year field, how can LFlights and RFlights be invalid? When I polled the whole database I got hundreds of results so I know that the database and connection data is fine, and the field DepartureDate exists too.

Edit: When I pulled all the contents of the DB, the dates came back in the format "YYYY-MM-DD". So I converted D to a string if it was <10, then 0D. Then built a string YYYY-MM-DD. Still no results. No error now, but no results. I handpicked values that I knew had records in them.

+1  A: 

Try enclosing Ldate and Rdate in the queries in single qoutes.

$LSQLRequest = "SELECT * FROM flight WHERE DepartureDate = '".$LDATE."'";
$RSQLRequest = "SELECT * FROM flight WHERE DepartureDate = '".$RDATE."'";

I also believe you do not need the # sign. That is usually an Access thing.

 $LDATE = $_REQUEST['LDateDay'] . '/' . $_REQUEST['LDateMonth'] . '/' . $_REQUEST['LDateYear'] ;
 $RDATE = $_REQUEST['RDateDay'] . '/' . $_REQUEST['RDateMonth'] . '/' . $_REQUEST['RDateYear'] ;
Josh
Thanks for this. You were right. I had another issue that the below commenter also grabbed but am gonna upvote you.
DeadMG
+1  A: 

MySQL only understands date literals in YYYY-MM-DD format, or a few slight variations. DD-MM-YYYY is not recognized by MySQL.

See more details at http://dev.mysql.com/doc/refman/5.1/en/datetime.html

Also as mentioned in another answer, date literals must be in single-quotes, like string literals.


If you don't include the quotes, the code you show looks like this:

SELECT * FROM flight WHERE DepartureDate = 2010/5/3.

The arithmetic expression 2010 divided by 5 divided by 3 is equal to 134.0. This is not going to match any date.

SELECT CURDATE() = 2010/5/3;

Returns false.

SELECT CURDATE() = '2010/5/3';

Returns true;

Bill Karwin
Single quotes was right. This answer was double right, and got both the issues. Thanks! Gonna accept as answer when the thing times out (6 minutes).
DeadMG
Thanks, I added more detail about the quotes issue so other readers will have a good explanation.
Bill Karwin