Jump to content

Compare date against row in mysql


xxclear

Recommended Posts

Ok, so i am trying to compare a date stored as a variable against a row in mysql, this is where i am so far:

 

 

I need to be able to search the table for:

 

1) a search keyword

2) a combination of a date, for example (just 2013) or (march-2012) or (7th of june) or (5th of september 2012) or (just the 16th) etc...

$result = mysqli_query($link, " SELECT id, auditor, auditee, datum, department, func, proc, audit_type, copy_to, comments

FROM audit_data

WHERE
(
`auditor` LIKE '%$search_field%' OR
`auditee` LIKE '%$search_field%' OR
`department` LIKE '%$search_field%' OR
`func` LIKE '%$search_field%' OR
`proc` LIKE '%$search_field%' OR
`audit_type` LIKE '%$search_field%')
OR
(`id` = '$radio_id')
OR
(datum = '$datum')
" )
OR 
die(mysqli_error($link));
 
  • Its only a small table otherwise I would be indexing.
  • The variable and row are both stored in native format (YYYY-MM-DD)

(If i put in a keyword into my $search_field it displays all the rows with a match, that works a treat).

 

The above example shows an exact match between the row and var which as it happen doesnt work. I have a calender setup on the input form and search form that will put in any date like this 2013-04-08 or 2013-00-08 etc. Logically this is what i want (unfortunately doesn't work):

if day($datum) = "00" {day($datum) = ""}
else { day($datum) = day($_REQUEST['date1'] }
if month($datum) = "00" {month($datum) = ""}
else { month($datum) = month($_REQUEST['date1'] }
if year($datum) = "0000" {year($datum) = ""}
else { year($datum) = year($_REQUEST['date1'] }

// select

select * from audit_data where `datum` = 
if(isset(day($datum))){day($datum)} OR
if(isset(month($datum))){month($datum)} OR
if(isset(year($datum))){year($datum)} OR
OR
[search keyword code here]
 

 

date1 being the calender output...

 

If the above seem odd, or simply wrong, forgive me, im just trying to get the point across as clear as i can.

Can anybody give me a head start on how to do this, any advice, links, examples, structure will be greatly appreciated!

Edited by xxclear
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.