Jump to content

select month from date


piheshpi90

Recommended Posts

This works. If it doesn't, you need to echo your query and see what the problem is with it. I suspect it's the 's' on the end that's causing you problems, since 's' will never equal 9.

SELECT * FROM aktiviti WHERE MONTH(`date_n`) = 9

i thought it will represent the value i insert in the form

 

$colname_Recordset3 = "-1";
if (isset($_POST['s'])) {
  $colname_Recordset3 = (get_magic_quotes_gpc()) ? $_POST['s'] : addslashes($_POST['s']);
}

$query_Recordset3 =sprintf( "SELECT * FROM aktiviti WHERE month(`date_n`) = s ", $colname_Recordset3);

 

i try SELECT * FROM aktiviti WHERE MONTH(`date_n`) = 9 and it work.but when i change it to s, it doesnt.

error say--> Unknown column 's' in 'where clause'.

 

 

That whole thing is a bit off. Since $_POST['s'] is being used to retrieve the month, and it's numeric, you should be validating it and casting it as an integer. Then in the sprintf(), you can use %d, since the value is expected to be an intger.

 

if( isset($_POST['s']) && ctype_digit($_POST['s']) ) {
     $colname_Recordset3 = (int) $_POST['s'];
} else {
     // you can trigger an error, set a default value, etc here if needed.
}

$query_Recordset3 =sprintf( "SELECT * FROM aktiviti WHERE month(`date_n`) = %d ", $colname_Recordset3);

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.