Jump to content

Recommended Posts

I am trying to query a database for range of result which fall between two dates.

 

The table i am selecting from is set up so that there is a column called date under which the time a row is inserted into the database is recorded by the NOW() function. The date is not stored as a unix time stamp but in the mysql formate, this might be a bad decision in terms of database design on my behalf.

 

here is the code for the query, it only returns one row of results whilst there are two and the difference in terms of dates is oneday:

 

$query = "SELECT ";
    $query .= "item1, item2, item3, item4, item5, item6, item7, item8, item9, item10 ";
    $query .= "FROM loot ";
    $query .= "WHERE ";
    $query .= "Date < NOW() ";
    $query .= "AND Date > DATE_ADD(NOW(), INTERVAL -1 MONTH) ";
    
    $result = mysql_query($query,$connection);
    confirm_query($result);

 

here is  what the screen print of phpmyadmin looks like:

db_loot.png

 

thanks in advance.

Link to comment
https://forums.phpfreaks.com/topic/164131-solved-select-between-a-range-of-dates/
Share on other sites

date is a reserved word in MySQL...you shouldn't use it as a column name. change it to something else if you can. otherwise, if you can't, use `backticks`:

 

$query = "SELECT ";

    $query .= "item1, item2, item3, item4, item5, item6, item7, item8, item9, item10 ";

    $query .= "FROM loot ";

    $query .= "WHERE ";

    $query .= "`Date` < NOW() ";

    $query .= "AND `Date` > DATE_ADD(NOW(), INTERVAL -1 MONTH) ";

 

    $result = mysql_query($query,$connection);

    confirm_query($result);

Hi thanks for replying, i managed to change date to picked on but i am still having the same problem. I think it has something to do with my date arithmetic. Would it have been better to use epoch format? I prefer the mysql format cause it means less time fiddling around with code converting one format to the next.

 

the code now looks like this:

$query = "SELECT ";
    $query .= "item1, item2, item3, item4, item5, item6, item7, item8, item9, item10 ";
    $query .= "FROM loot ";
    $query .= "WHERE ";
    $query .= "picked_on < NOW() ";
    $query .= "AND picked_on > DATE_ADD(NOW(), INTERVAL -1 MONTH)";
    
    $result = mysql_query($query,$connection);
    confirm_query($result);

 

 

Thanks for the reply but using CURDATE() doesn't seem to work either.

 

it now looks like this.

 

$query = "SELECT ";
    $query .= "item1, item2, item3, item4, item5, item6, item7, item8, item9, item10 ";
    $query .= "FROM loot ";
    $query .= "WHERE ";
    $query .= "picked_on < CURDATE() ";
    $query .= "AND picked_on > DATE_ADD(CURDATE(), INTERVAL -1 MONTH)";
    
    $result = mysql_query($query,$connection);
    confirm_query($result);
    $loot = mysql_fetch_array($result,MYSQL_ASSOC);

 

Now i am think if i can use a different query to get the results i want.

Ok i tried something typing the dates into the query and i still get the same problem. I dont know whats wrong and i am starting to get really frustrated. It is still returning only one row when there are two rows within the specified dates.

 

here is how it looks now.

      $query = "SELECT * ";
    /*$query .= "item1, item2, item3, item4, item5, item6, item7, item8, item9, item10 ";*/
    $query .= "FROM loot ";
    $query .= "WHERE ";
    $query .= "picked_on > '2009-06-01' ";
    $query .= "AND picked_on < '2009-07-30'";
    
    $result = mysql_query($query,$connection);
    confirm_query($result);
    $loot = mysql_fetch_array($result,MYSQL_ASSOC);
    
    print_r($loot);

 

I also tried the following and still the same problem.

  $query = "SELECT * ";
    /*$query .= "item1, item2, item3, item4, item5, item6, item7, item8, item9, item10 ";*/
    $query .= "FROM loot ";
    $query .= "WHERE ";
    $query .= "picked_on BETWEEN ";
    $query .= "'2009-06-01' AND '2009-07-30'";
    
    $result = mysql_query($query,$connection);
    confirm_query($result);
    $loot = mysql_fetch_array($result,MYSQL_ASSOC);
    
    print_r($loot);

 

A print_r of $loot for both of the above gives this:

Array ( [id] => 1 [picked_on] => 2009-06-28 [member_name] => tom [item1] => Ebonite [item2] => Aluminite [item3] => Kobalit [item4] => Dragoturkey Peak [item5] => Bear Bone [item6] => Black Dragoss Horn [item7] => Ice Kwak Beak [item8] => Dragoturkey [item9] => Barley [item10] => Barley Sugar ) 

 

Only the first row is returned as you can see, is there something wrong with my query?

 

thanks in advance.

Up until reply #4 in this thread, you did not post anything showing how you are retrieving the data.

 

I suggest reading the php.net documentation for the mysql_fetch_array function you are using - http://us.php.net/mysql_fetch_array

 

Forgive but i tried using mysql_fetch array on it's own with the only parameter being $result, i also tried MYSQL_NUM AND MYSQL_ASSOC as you seen in the above post but still only the first row is returned. This is why i think there is a problem with the  query.

 

The reason why i used MYSQL_ASSOC  is because i am using array_count_values to count the numbers of times something is repeated in the array and using fetch_array without any parameter caused double counting since it return both an associative and number indicies.

 

I am very new to php(only 3 months) and programming is general, in fact mysql is the only programme apart from a little javascript i know.

 

I really want to find a solution to this, but i might not know whats wrong or i am not explaining it clear enough.

 

 

The point of the link to the documentation was that mysql_fetch_array -

 

"Fetch a result row as an associative array, a numeric array, or both"

 

"Returns an array that corresponds to the fetched row and moves the internal data pointer ahead."

 

"Returns an array of strings that corresponds to the fetched row, or FALSE  if there are no more rows."

 

All of these state row (singular), not rows (plural) and the code examples at that link all use a while() loop to continue fetching a row until a FALSE value is returned.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.