Medoo限制不起作用

时间:2017-11-20 18:15:42

标签: php medoo

我遇到了意想不到的问题

<?php
    header("Access-Control-Allow-Origin: *");
    include_once("Medoo.php");
    use Medoo\Medoo;

    $db = new Medoo([
        'database_type' => 'mysql',
        'database_name' => '************',
        'server' => 'localhost',
        'username' => '************',
        'password' => '************'
    ]);

    function getThreads($db, $start, $limit){
        $data = $db->select('threads', ['id', 'title', 'content', 'date', 'userid'],  ["AND" => ["active" => "1", "limit" => [$start, $limit]]]);
        return $data;
    }

    function utf8ize($d) {
        if (is_array($d)) {
            foreach ($d as $k => $v) {
                $d[$k] = utf8ize($v);
            }
        } 
        else if (is_string ($d)) {
            return utf8_encode($d);
        }
        return $d;
    }

    $start=htmlspecialchars(trim($_POST['start']));
    $limit=htmlspecialchars(trim($_POST['limit']));

    $query = getThreads($db, $start, $limit);
    if($query == false) {
        echo json_encode(array("code" => "failed"));
    } 
    else {
        echo json_encode(utf8ize($query));
    }
?>

如果我通过参数start 0和limit 5的ajax调用php文件,我总是从console.log()获得“失败”;结果。已经尝试过medoo的debug()函数,但是sql查询看起来不错。没有限制一切正常......任何想法或建议?有人可以帮忙吗?

var start = 0;
var limit = 5;
$.ajax({
    type: "POST",
    url: 'http://*********/****/********.php',
    crossDomain: true,
    dataType: "text json",
    data: "&start="+start+"&limit="+limit,
    success: function (data) {
        console.log(data);
        if(data.code == "failed"){

        } else {
            start += limit;
            data.reverse();
            $('#threads').html("");
            for (var i = 0; i < data.length; i++) {
                if(data[i]['userid'] == localStorage.id){
                    $('#threads').append("<ons-card onclick='pushpc("+data[i]["id"]+","+data[i]["userid"]+");'><div id='threadid'>"+data[i]["id"]+"</div><div class='title'>"+decode_utf8(data[i]["title"])+"</div><div class='content'>"+decode_utf8(data[i]["content"])+"</div></ons-card>");
                } else {
                $('#threads').append("<ons-card onclick='pushpc("+data[i]["id"]+","+data[i]["userid"]+");'><div id='threadid'>"+data[i]["id"]+"</div><div class='title'>"+decode_utf8(data[i]["title"])+"</div><div class='content'>"+decode_utf8(data[i]["content"])+"</div></ons-card>");
            }
            }
        }
    }
});

2 个答案:

答案 0 :(得分:2)

应该是

   function getThreads($db, $start, $limit){
    $data = $db->select('threads', ['id', 'title', 'content', 'date', 'userid'],  ["active" => "1", "LIMIT" => [$start, $limit]]);
    return $data;
   }

LIMIT不是medoo中的条件,您可以在没有&#34; AND&#34;

的情况下使用它

答案 1 :(得分:-1)

我解决了它:

  $data = $db->query("SELECT id, title, content, date, userid FROM threads LIMIT ".$start.", ".$limit."")->fetchAll();