我正在尝试将Mysqli代码转换为PDO for Ajax无限Scroll系统我已经在互联网上找到了,所以我可以将它添加到我正在学习php的博客中。 这是原始的ajax.php文件:
if( isset( $_POST['start'] ) && isset( $_POST['limit'] ) && !empty( $_POST['start'] ) && !empty( $_POST['limit'] ) ){
$start = $_POST['start'];
$limit = $_POST['limit'];
$query = "SELECT * FROM timeline limit $start, $limit";
$result = mysqli_query($con, $query) or die('Error: ' . mysqli_error($con));
$data = array();
$rowcount = mysqli_num_rows($result);
$data['count'] = $rowcount;
while($row = mysqli_fetch_assoc($result)) {
$data['content'][] = $row;
}
mysqli_close($con);
echo json_encode($data);exit;
}
这是我的尝试,一切看起来都不错,但它仍然没有用,所以我想我搞砸了一些PDO功能......
if( isset( $_POST['start'] ) && isset( $_POST['limit'] ) && !empty( $_POST['start'] ) && !empty( $_POST['limit'] ) ){
$start = $_POST['start'];
$limit = $_POST['limit'];
$stmt = "SELECT * FROM timeline limit $start, $limit";
$data = array();
$rowcount = $stmt->fetchAll();
$data['count'] = $rowcount;
foreach ($db->query($stmt) as $row) {
$data['content'][] = $row;
}
echo json_encode($data);
}
我知道我应该使用PDO准备好的语句而且我会,但首先我想让它按原样运行......
这是Ajax script.js本身:
flag = true;
$(window).scroll(function() {
if($(window).scrollTop() + $(window).height() == $(document).height()){
first = $('#first').val();
limit = $('#limit').val();
no_data = true;
if(flag && no_data){
flag = false;
$('#loader').show();
$.ajax({
url : 'ajax.php',
dataType: "json",
method: 'post',
data: {
start : first,
limit : limit
},
success: function( data ) {
flag = true;
$('#loader').hide();
if(data.count > 0 ){
first = parseInt($('#first').val());
limit = parseInt($('#limit').val());
$('#first').val( first+limit );
$('#timeline-container');
$.each(data.content, function(key, value ){
if(value.event!=''){
html = '<li class="timeline-item">';
html += '<div class="timeline-badge" data-
toggle="popover" data-placement="left" data-
trigger="hover" data-
content="Mention"><a href="#"></a></div>';
html += '<div class="timeline-panel">';
html += '<div class="timeline-heading">';
html += '<p>This is the new post: </p>';
html += '<div class="timeline-date"><i class="fa fa-
calendar-o"></i> '+value.date+'</div>';
html += '</div>';
html += '<div class="timeline-content">';
html += '<p>'+value.post+'</p>';
html += '</div>';
html += '</li>';
}
$('#timeline-container').append( html );
$('.timeline-item').waypoint({
triggerOnce: true,
offset: '80%',
handler: function() {
jQuery(this).addClass('animated fadeInUp');
}
});
});
}else{
alert('No more data to show');
no_data = false;
}
},
error: function( data ){
flag = true;
$('#loader').hide();
no_data = false;
alert('Something went wrong, Please contact admin');
}
});
}
} });
这是我的配置文件,以备需要时...提前感谢大家!
define('DBHOST','localhost');
define('DBUSER','root');
define('DBPASS','');
define('DBNAME','my_db');
$db = new PDO("mysql:host=".DBHOST.";charset=utf8;dbname=".DBNAME, DBUSER,
DBPASS);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
define('FIRST', 4);
define('LIMIT', 4);
答案 0 :(得分:1)
这一行是完全错误的:
$rowcount = $stmt->fetchAll();
$stmt
是一个字符串,而不是PDO对象,您无法在其上调用fetchAll()
。并且fetchAll()
返回结果的所有行,而不是计数。
if( isset( $_POST['start'] ) && isset( $_POST['limit'] ) && !empty( $_POST['start'] ) && !empty( $_POST['limit'] ) ){
$start = $_POST['start'];
$limit = $_POST['limit'];
$data = array();
$stmt = $db->prepare("SELECT * FROM timeline limit :start, :limit";
$stmt->execute(array(':start' => $start, ':limit' => $limit));
$data['content'] = $stmt->fetchAll();
$data['count'] = count($data['content']);
}
echo json_encode($data);