MYSQL, WHERE / LIMIT / ORDERBY Combination

I have it

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

which works great however when i change this to

$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 the order of something like this

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

what am I doing wrong?

+3
source share
2 answers

You need to derive the limit clause from the DATE () macro.

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

Also, if you want to order, you need to set a field, for example

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

Please note that the actual error you receive is how you invoke the mysqli_errorerror handling code. Fix it too.

+2
source

Source: https://habr.com/ru/post/1786672/


All Articles