简体   繁体   中英

jQuery and php. mysql error

My code loads with the following text on top of it: "Problem with SQL: SELECT * FROM table WHERE id < ORDER BY id DESC LIMIT 5" Could someone help me find a solution? Thanks

//jQuery
 var value = '3';        
    $.post("load.php", {number: value} ,function(data){
    $('p').append(data);
    });          
    $('p').load('load.php');

//PHP load.php
//I have the escape inside $db.
$random = $_POST['number'];
$db->query('SELECT * FROM table WHERE id <' . '$random' . 'ORDER BY id DESC LIMIT 1');

$result = $db->get();       
        foreach ($result as $key => $value){ 
        echo $value['user'];
};

//Output 
Problem with SQL: SELECT * FROM table WHERE id < ORDER BY id DESC LIMIT 5
$value['user']

Try changing it to this one:

$db->query("SELECT * FROM table WHERE id < " . $random . " ORDER BY id DESC LIMIT 1");

Better use double instead of single quotes.

The reason this is not working is because single quotes does not allow PHP to expand the variable value.

So instead of this:

$db->query('SELECT * FROM table WHERE id <' . '$random' . 'ORDER BY id DESC LIMIT 1');

You might do this:

$db->query('SELECT * FROM table WHERE id <' . $random . ' ORDER BY id DESC LIMIT 1');

Just remove the quotes in $random and you'll be well.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM