Medoo 限制不起作用
Medoo Limit not working
我遇到了意想不到的问题
<?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));
}
?>
如果我通过 ajax 使用参数开始 0 和限制 5 调用 php 文件,我总是从 console.log() 得到 "failed";结果。已经尝试过 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>");
}
}
}
}
});
我解决了:
$data = $db->query("SELECT id, title, content, date, userid FROM threads LIMIT ".$start.", ".$limit."")->fetchAll();
应该是
function getThreads($db, $start, $limit){
$data = $db->select('threads', ['id', 'title', 'content', 'date', 'userid'], ["active" => "1", "LIMIT" => [$start, $limit]]);
return $data;
}
LIMIT 不是 medoo 中的条件,您可以在没有 "AND"
的情况下使用它
我遇到了意想不到的问题
<?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));
}
?>
如果我通过 ajax 使用参数开始 0 和限制 5 调用 php 文件,我总是从 console.log() 得到 "failed";结果。已经尝试过 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>");
}
}
}
}
});
我解决了:
$data = $db->query("SELECT id, title, content, date, userid FROM threads LIMIT ".$start.", ".$limit."")->fetchAll();
应该是
function getThreads($db, $start, $limit){
$data = $db->select('threads', ['id', 'title', 'content', 'date', 'userid'], ["active" => "1", "LIMIT" => [$start, $limit]]);
return $data;
}
LIMIT 不是 medoo 中的条件,您可以在没有 "AND"
的情况下使用它