-1

I'm trying to create a query that takes the users input from a text field called $incorporation_date.

Query Idea

$sql = "SELECT * FROM companies WHERE incorporation_date LIKE '%%%%/%%/%%" . $incorporation_date . "%%%%/%%/%%'";

How would I make it so that you could use SQL to bring up the values of the submitted format you enter.

Example search 2015-06-15

1 Answers1

1

Use a Date or Datetime and spare yourself of the grief that would otherwise follow were it not

drop table theGuy;

create table theGuy
(
    id int not null auto_increment primary key,
    fullName varchar(60) not null,
    birthDate date not null
);

insert theGuy (fullName,birthDate) values ('Kim Smithers','2002-3-1'),('John Doe','2014-4-5');

select * from theGuy where birthDate>='2000-1-1' and birthDate<='2007-12-31';

select * from theGuy where birthDate between '2000-1-1' and '2007-12-31';


select *,birthDate+interval 45 DAY as BirthCertAvail from theGuy;

select *,datediff(curdate(),birthDate) as DaysAlive from theGuy;

You might find the built-in routines adequate, such as intervals, without having to rewrite them. ;)

AsConfused
  • 325
  • 2
  • 7
  • Cool you are using datetime. Some wacky homegrown schemas and bizarreness from some peeps – AsConfused Jun 15 '15 at 13:51
  • To get datepart of `datetime` that may contain not stroke of midnight time in it use function date() – AsConfused Jun 15 '15 at 13:56
  • can you not have it to search a custom date range? from user input – Badge Nation Jun 15 '15 at 13:58
  • You can but you need to cleanse it especially when using mysql_* deprecated technology. Search on sql injection ...http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php – AsConfused Jun 15 '15 at 14:00