query a record that matches today's date

Discussion in 'MySQL' started by soldbychris, Sep 26, 2008.

  1. #1
    How do you query a record for today's date. Here is what I have but it does not work.


    // Retrieve all the data from the "Date" table
    $result = mysql_query("SELECT curdate()")
    or die(mysql_error());

    // store the record of the "Date" table into $row
    $row = mysql_fetch_array( $result );
    // Print out the contents of the entry


    Any assistance would be appreciated.
     
    soldbychris, Sep 26, 2008 IP
  2. jestep

    jestep Prominent Member

    Messages:
    3,659
    Likes Received:
    215
    Best Answers:
    19
    Trophy Points:
    330
    #2
    $result = mysql_query("SELECT curdate() AS today")
    or die(mysql_error());

    // store the record of the "Date" table into $row
    $row = mysql_fetch_array( $result );

    //SHOULD BE AVAILABLE AS $row['today'];


    I'm not totally sure on what you're trying to accomplish, because curdate() just returns the server's current date. It isn't field dependant. Are you trying to retrieve the date of a record in the table.
     
    jestep, Sep 26, 2008 IP
  3. soldbychris

    soldbychris Peon

    Messages:
    42
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #3
    I have dated records in the database and would like to only show the record that matches today's date.
     
    soldbychris, Sep 26, 2008 IP
  4. jestep

    jestep Prominent Member

    Messages:
    3,659
    Likes Received:
    215
    Best Answers:
    19
    Trophy Points:
    330
    #4
    What is the column name of the date field and the column data type (date, datetime, timestamp, etc...)?

    It would be something like (This may vary depending on the data type):
    SELECT column_names... FROM my_table WHERE DATE_FORMAT(date_column, '%Y-%m-%d') = CURDATE();
     
    jestep, Sep 26, 2008 IP
  5. soldbychris

    soldbychris Peon

    Messages:
    42
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #5
    The column field is "date" and it is listed on the server in date format yyyy-mm-dd

    The confusing part of is that I named the table "Date" and I have a column name "Date" for the date of the record. Should I change the table name.
     
    soldbychris, Sep 26, 2008 IP
  6. soldbychris

    soldbychris Peon

    Messages:
    42
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Here is the entire script, if that helps

    <html>

    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
    <title></title>
    </head>

    <body>
    <?php
    // Make a MySQL Connection
    mysql_connect("secureserver.net", "name", "xxxxxxx") or die(mysql_error());
    mysql_select_db("viewtext") or die(mysql_error());

    // Retrieve all the data from the "Date" table
    $result = mysql_query("SELECT * FROM Date")
    or die(mysql_error());

    // store the record of the "Date" table into $row
    $row = mysql_fetch_array( $result );
    // Print out the contents of the entry


    echo "$Date: ".$row['Date'];
    echo "$Email: ".$row['Email'];
    echo "$Par1:".$row['Par1'];
    echo "$Par2:".$row['Par2'];
    echo "$Par3:".$row['Par3'];
    echo "$Par4:".$row['Par4'];
    echo "$Par5:".$row['Par5'];
    ?>
    </body>

    </html>
     
    soldbychris, Sep 26, 2008 IP
  7. jestep

    jestep Prominent Member

    Messages:
    3,659
    Likes Received:
    215
    Best Answers:
    19
    Trophy Points:
    330
    #7
    It's probably not a good idea to use that name for the table and field name but it should work if you use backticks when querying on that table/field.

    Select `date` FROM `date` WHERE `date`... etc...
     
    jestep, Sep 26, 2008 IP
  8. soldbychris

    soldbychris Peon

    Messages:
    42
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #8
    I am trying to display the entire row of data where the date in one field matches the current date. This is what I have but I am stuck, it only returns the date.


    <body>
    <?php
    // Make a MySQL Connection
    mysql_connect("secureserver.net", "database", "xxxxxxxx") or die(mysql_error());
    mysql_select_db("database") or die(mysql_error());

    // Retrieve all the data from the "Emailing" table
    $result = mysql_query("SELECT `Date` FROM `Emailing` WHERE DATE_FORMAT(Date,'%Y-%m-%d') = CURDATE()")
    or die(mysql_error());

    // store the record of the "Date" table into $row
    $row = mysql_fetch_array( $result );
    // Print out the contents of the entry

    echo "<h1>Real Estate Term of the Day</h1>";
    echo "$Date".$row['Date'];
    echo "<br>";
    echo "<br>";
    echo "$Par1".$row['Par1'];
    echo "<br>";
    echo "<br>";
    echo "$Par2".$row['Par2'];
    echo "<br>";
    echo "<br>";
    echo "$Par3".$row['Par3'];
    echo "<br>";
    echo "<br>";
    echo "$Par4".$row['Par4'];
    echo "<br>";
    echo "<br>";
    echo "$Par5".$row['Par5'];
    echo "<br>";
    echo "<br>";
    ?>
    </body>
     
    soldbychris, Sep 26, 2008 IP
  9. SEMSpot

    SEMSpot Peon

    Messages:
    513
    Likes Received:
    25
    Best Answers:
    0
    Trophy Points:
    0
    #9
    Use this when querying your db. The cur_date simply gets the current date for you from the users computer.

    $cur_date = date("Y/m/d");

    // Retrieve all the data from the "Emailing" table
    $result = mysql_query("SELECT * FROM Emailing WHERE DATE = '$cur_date'");
     
    SEMSpot, Sep 27, 2008 IP