i am trying to write one function which return sql statement
like
function get_sql($name=0,$date_start=0,$date_end=0)
{
$addQuery=" where 1=1";
if($name>0)
{
$addQuery .=" and name=".$name;
}
if($date_start>0)
{
$addQuery.=" and date >=".$date_start;
}
if($date_end >0)
{
$addQuery.=" and date<=".$date_end;
}
$query="select * from TABLE_ARTICLE".$addQuery;
return $query;
}
sorry guys for wrong syntax typing
i have two concerns about this function.
- does this is proper approach or not?
- does this function will work when date will be passed in
01/03/2012format, as you can see i want result between two dates if both are selected and after first date or before end date ?
i mean does this is best way to get data from sql in dates?
Thanks for helping
You might be better off using implode
Note also that I have quoted all your values as none of them seem to be integer values.
As regards the date format, MySQL will not recognise the format you state. You will need to provide the dates in YYYY-MM-DD (or possibly unix timestamp) formats to MySQL.