drkrsx3135168 2017-11-20 18:15
浏览 86
已采纳

Medoo限制不起作用

I have faced an unexpected problem

<?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));
    }
?>

If I call the php file through ajax with parameters start 0 and limit 5, I get always "failed" from console.log(); as result. Already tried debug() function from medoo but the sql query looks good. Without Limit everything works fine... Any ideas or suggestions? Can somebody help?

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条回答 默认 最新

  • doutuoji8418 2017-11-20 20:50
    关注

    I solved it:

      $data = $db->query("SELECT id, title, content, date, userid FROM threads LIMIT ".$start.", ".$limit."")->fetchAll();
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 2024-五一综合模拟赛
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭