PHP script SELECT FROM mysql database WHERE date

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • pizzy

    PHP script SELECT FROM mysql database WHERE date

    Problem:

    I am trying to develope my personal site to select from my mysql
    database and organize data on the page so that it will display all the
    submissions for that day, then break and display the next day, then
    break and display the third day, etc...


    Question:

    Should I query the whole database into an array and then sort with php?
    Example: SELECT * FROM pictures

    Or should I (and I would prefer) query an array with the dates I need?
    Example: SELECT * FROM pictures WHERE date = $array[$i]

    Can you provide an example of how the php script would look? I am
    looking for some types of special functions I might be missing from php
    or mysql.

    Any help will be greatly appreciated.


    Thanks,

    pizzy

  • Samuel

    #2
    Re: PHP script SELECT FROM mysql database WHERE date

    In your first example you'd have to sort the data with php, and in the
    second you'd have to query the database once for each date you want to
    show. I'd recommend this instead;

    SELECT * FROM pictures ORDER BY date

    You will be able to get all the data you need in only one query. The
    script would basically look like this.

    <?php
    //Assuming you already opened and selected the db...
    $query = "SELECT * FROM pictures ORDER BY date";
    $result = mysql_query ($query);

    while (($number_of_it ems < 30) && ($row = mysql_fetch_arr ay($result)))
    {
    if ($last_date != $row['date'])
    print_break_bet ween_dates_or_w hatever();

    print_the_data_ or_whatever($ro w);

    $last_date = $row['date'];
    $number_of_item s++;
    }

    ?>

    Hope I helped and all =)

    Comment

    Working...