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
  • 6,135
  • 11
  • 52
  • 88
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,267
  • 3
  • 15
  • 38
Nithin
  • 5,470
  • 37
  • 44
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
  • 580
  • 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,267
  • 3
  • 15
  • 38
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
  • 487
  • 3
  • 8