Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MYSQL, combination of WHERE/LIMIT/ ORDERBY

Tags:

mysql

limit

where

I have this

$sql = 'SELECT * FROM tb_event WHERE DATE(edate) >= DATE(NOW())';
   $result = $conn->query($sql) or die(mysqli_error());
   $news = $result->fetch_assoc();

which runs fine however when I change it to this

$sql = 'SELECT * FROM tb_event WHERE DATE(edate) >= DATE(NOW() LIMIT 2)';

I get this error message

Warning: mysqli_error() expects exactly 1 parameter, 0 given in /var/www/.../...php

Finally I would like to combine it with order by that is something like this

$sql = 'SELECT * FROM tb_event WHERE DATE(edate) >= DATE(NOW() LIMIT 2 ORDER BY DESC)';

what am I doing wrong?

like image 998
Ilias Avatar asked Jan 20 '11 13:01

Ilias


1 Answers

You need to pull the limit clause out of the DATE() macro.

SELECT * FROM tb_event WHERE DATE(edate) >= DATE(NOW()) LIMIT 2

Also if you want to order you have to set a field, like

SELECT * FROM tb_event WHERE DATE(edate) >= DATE(NOW()) LIMIT 2 ORDER BY myField DESC
like image 191
josh.trow Avatar answered Nov 03 '22 00:11

josh.trow