J. Doe Cd
J. Doe Cd

Reputation: 69

mysqli not work with php date

I have a table with "data" column in varchar

enter image description here

and value "21/10/2017"

enter image description here

Now when try a query like:

$data = mysqli_real_escape_string($bd, date("j/m/y"));      
        $result = mysqli_query($bd, "SELECT * FROM tracking where data='".$data."'");

This query return 0, but if change data with random string like "dog" and replace data from mysql with dog the query work fine but i need the date not the dog.

If use $data = "21//10//2017"; thw query work why?

Upvotes: 0

Views: 623

Answers (2)

FluffyKitten
FluffyKitten

Reputation: 14312

You are using the wrong date format in date. You are getting the date as j/m/y which is in the format d/mm/yy e.g. 21/10/17 but your date/strings in the database are in the format dd/mm/yyyy.

To get the years in the full 4-digit version, you need to use Y instead of y, i.e.

$data = mysqli_real_escape_string($bd, date("j/m/Y"));   

Note: You might also have the month and day formats wrong - you are using the single digit day and 2-digit month representation in PHP (e.g. 1/01/17 instead of 01/01/17) but your strings might use the full 2 digit - we can't tell in your date example... in that case you will need:

$data = mysqli_real_escape_string($bd, date("d/m/Y"));   

Upvotes: 1

user1597430
user1597430

Reputation: 1146

I see that you use j/m/y, according to date manual y returns year with last 2 digits, not 4 digits as you expect so use Y instead.

But to be honest you have to store date in the date, datetime or timestamp types, not in varchar.

Upvotes: 1

Related Questions