I just read another recent thread about checking the SQL statements in phpMyAdmin. I did that and my SQL statements returned zero rows when I actually have one record in the database that satisfies the condition
With respect, if it returns 0 rows, then it appears that your SQL does not satisfy the condition where a single row should be returned. Use backticks - I think DATE is a reserved word.
SELECT `date`,`item`,`price` FROM `expenses` WHERE `ic_no`='$ic' AND SUBSTRING(`date`,3,-5) = '$substrMnth' AND SUBSTRING(`date`,6) = '$substrYear'"
diafol
Keep Smiling
10,655 posts since Oct 2006
Reputation Points: 1,628
Solved Threads: 1,510
Skill Endorsements: 57
echo the SQL statement and print it out here:
echo "SELECT `date1`,`item`,`price` FROM `expenses` WHERE `ic_no`='$ic' AND SUBSTRING(`date1`,3,-5) = '$substrMnth' AND SUBSTRING(`date1`,6) = '$substrYear'";
BTW - you'd probably be better extracting the data in php and creating a date in the DB format in order to dispense with teh mysql SUBSTRINGs. You can use the BETWEEN ... AND ... syntax to get all dates between two limits.
This looks needlessly complicated.
diafol
Keep Smiling
10,655 posts since Oct 2006
Reputation Points: 1,628
Solved Threads: 1,510
Skill Endorsements: 57
The date format you should employ is yyyy-mm-dd as you can use > or < with it.
For retrieving all records for a particular month:
Say you want month 04 (April) and year 2011:
//if $month = '04' (string) or $month = 4 (int)
if($month == '12'){
$month2 = '01'
$year2 = $year + 1;
}else{
$month2 = str_pad(intval($month)+1,2,'0',STR_PAD_LEFT);
$year2 = $year;
}
$r = mysql_query("SELECT `date1`,`item`,`price` FROM `expenses` WHERE `ic_no`='900314106355' AND `date1` >= '{$year)-{$month}-01' AND `date1` < '{$year2}-{$month2}-01'");
diafol
Keep Smiling
10,655 posts since Oct 2006
Reputation Points: 1,628
Solved Threads: 1,510
Skill Endorsements: 57
Question Answered as of 1 Year Ago by
diafol I know you've solved this, but I couldn't help thinking that there was an easier way to do this - use MONTH and YEAR:
$month = intval($_POST['month']);
$year = intval($_POST['year']);
$results = mysql_query("SELECT `date1`,`item`,`price` FROM `expenses` WHERE `ic_no`='$ic' AND MONTH(`date1`) = $month AND YEAR(`date1`) = $year");
That's been bugging me for a while, but I couldn't find this thread! :)
http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html
diafol
Keep Smiling
10,655 posts since Oct 2006
Reputation Points: 1,628
Solved Threads: 1,510
Skill Endorsements: 57