0

I am trying to run this sql query in php but I am getting an error that my query is wrong. please help

<?php
    @include("dbcon.php");
    $last_date = $_POST['lastdate'];
    $page_rows=3;
    $result = mysql_query("SELECT  * from events where dated < $last_date order by dated desc limit $page_rows") or die(mysql_error());
    //for testing
    echo $result;
?>
Oliver Bayes-Shelton
  • 5,911
  • 11
  • 47
  • 86
Yash Sanap
  • 21
  • 6

4 Answers4

0

i think its because of your $last_date ..It should be inside single quotes '' this should work

$result = mysql_query("SELECT  * from events where dated < '$last_date' order by dated desc limit $page_rows")  or die(mysql_error());
Utkarsh Dixit
  • 4,153
  • 3
  • 14
  • 36
Nithin
  • 4,672
  • 32
  • 43
0

If its only syntax error then try this.

<?php
    @include("dbcon.php");
    $last_date = $_POST['lastdate'];
    $page_rows=3;
    $result = mysql_query("SELECT  * from events where dated < '$last_date' order by dated desc limit '$page_rows'") or die(mysql_error());
    //for testing
    echo $result;
    ?>
Tanjima Tani
  • 560
  • 4
  • 18
0

Just add single quotes. Use the below Code

<?php
@include("dbcon.php");
$last_date = $_POST['lastdate'];
$page_rows=3;
$result = mysql_query("SELECT  * from events where dated < '$last_date' order by dated desc limit $page_rows") or die(mysql_error());
//for testing
echo $result;
?>

Hope this helps you

Utkarsh Dixit
  • 4,153
  • 3
  • 14
  • 36
0
$result = mysql_query(
            sprintf("SELECT  * 
                    FROM events
                    WHERE dated < '%s' 
                    ORDER BY dated DESC 
                    LIMIT %d", addslashes($last_date), intval($page_rows))) 
    or die(mysql_error());
Jiri Zachar
  • 477
  • 3
  • 7