Gaspar
Gaspar

Reputation: 63

string to date not working (php & MySQL

I have a column in MySQL where date is a string (d/m/Y). when I try to get this table ordered by date I get an internal server error and nothing happens:

$pdo->prepare('SELECT IdOrder,Name,Lname,Phone,Date FROM ordenes WHERE Lname LIKE ? ORDER BY STR_TO_DATE(Date, '%d/%m/%Y') DESC LIMIT 50');

What am I doing wrong?

Upvotes: 0

Views: 65

Answers (1)

Kurohige
Kurohige

Reputation: 1418

maybe this works

$pdo->prepare("SELECT IdOrder,Name,Lname,Phone,Date FROM ordenes WHERE Lname LIKE ? ORDER BY STR_TO_DATE(Date, '%d/%m/%Y') DESC LIMIT 50");

You are using single quotes both in query and date format so you get PHP error. Another way is using backslashs:

$pdo->prepare('SELECT IdOrder,Name,Lname,Phone,Date FROM ordenes WHERE Lname LIKE ? ORDER BY STR_TO_DATE(Date, \'%d/%m/%Y\') DESC LIMIT 50');

Upvotes: 1

Related Questions