动态mysql查询LIMIT不起作用

时间:2011-11-27 07:22:46

标签: mysql

我正在使用PHP变量创建限制mysql查询的参数。我正在输出查询以确保它是正确的,它是。但是结果完全忽略了offset参数。但是,如果我手动键入LIMIT,它可以正常工作。

    $page_rows = 5; 
    $max = ($pagenum - 1) * $page_rows .',' .$page_rows; 

    $qry = "SELECT * FROM ".$wpdb->prefix."nslikethis_votes WHERE user_id = '$uid' AND active = 1 ORDER BY time DESC LIMIT " . $max;

    $rs = mysql_query($qry);

    $result = array();

    if($rs && $rows>0){
        while($lc = mysql_fetch_object($rs, "LikedContent")){
            $result[] = $lc;
        }       
    }

    return $result;

输出$ qry给我这个,无论我使用$ max还是手动输入'5,5':

SELECT * FROM wp_nslikethis_votes WHERE user_id = '1' AND active = 1 ORDER BY time DESC LIMIT 5,5

1 个答案:

答案 0 :(得分:0)

尝试这样做以便检查:

$page_rows = "5";
$max = ($pagenum - 1) * $page_rows .',' .$page_rows; 
$qry = "SELECT * FROM ".$wpdb->prefix."nslikethis_votes WHERE user_id = '$uid' AND `active` = 1 ORDER BY `time` DESC LIMIT " . $max;

//check for variables in query like $wpdb->prefix, $uid: are they fine, and try adding tilded sign as above

希望这适合你