Jump to content

get month from database


mraza

Recommended Posts

Hi, I m trying to extract data by month from my date table.

my date format 2011-03-28

 

my query

$sql = "select * from table WHERE MONTH(date) = 03 " 

 

Now problem is i have many records in 3rd month but it only display the first row with above $sql. How can i see all month records.

Thanks

Link to comment
Share on other sites

Where is your code to display the results. The problem may be in that code. Also, do you really want to show all records from a specific month or do you really want to show the records from a specific MONTH & YEAR? That query above would get records from March of any year.

Link to comment
Share on other sites

thanks for reply sir,

here is my code

$sql= $dbc->dbRunSql("select * from points WHERE MONTH(date) = '03' " );
$records = mysql_fetch_assoc($sql);
print_r($records);

i am doing a print_r and i see only one row

Array
(
    [id] => 1
    [userid] => 2
    [points] => 1000
    [date] => 2011-03-05
    [month] => 0
)

only above shows but i have more records in 03 Month.

Edit: i wants to show record from a month but like to know for year too. thanks

Link to comment
Share on other sites

Well, I just tested it several different ways and it worked for me.

SELECT * FROM `scan_jobs` WHERE MONTH(`started`) = 3
SELECT * FROM `scan_jobs` WHERE MONTH(`started`) = '03'
SELECT * FROM `scan_jobs` WHERE MONTH(`started`) = "03"

 

The problem must be in your data. Is the field set as a date field and did you validate that the values you expect for those records is correct? if you have a page where you are entering the records you might have a bug that is not saving the dates correctly.

 

EDIT: Scratch that

 

You still need to run a mysql_fetch for EACH record. The first one will only get the first record!!!

 

$sql= $dbc->dbRunSql("select * from points WHERE MONTH(date) = '03' " );
while($record = mysql_fetch_assoc($sql))
{
    print_r($record);
}

Link to comment
Share on other sites

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.