PHP通过JQuery请求MYSQL

标签 php jquery mysql ajax

我有一个图片库,目前我将结果限制为 10 个。

我想通过 jQuery 做的是单击“加载更多结果”按钮, 在数据库中加载接下来的 10 行(图像)。

目前我使用类 query.php 进行查询调用,然后在我的 index.php 中用 foreach 循环显示数据。

我在想也许可以使用查询字符串来更改点击时第 10-20 行的限制,但在这方面遇到了困难。

我们将不胜感激。

类query.php

     public function Query($db_table, $limit) {

        $this->db_query = mysql_query("SELECT * FROM $db_table ORDER BY time DESC limit $limit");

        while($row = mysql_fetch_array($this->db_query)) {
            $rows[] = $row;
        }
        return $rows;
  }    

索引.php

$lim = $_GET['limit'];
$Results = $Database->Query('info',  $lim); 
    if(is_array($Results)) {?>
   foreach ($Results as $Result) { 
  //echo results

j查询

$("#my_container").load("../lib/class.database.php?limit=10,20"  );

亲切的问候, 亚当

最佳答案

很简单:

1.) 您对 PHP 脚本进行 AJAX 调用

$.ajax(
{
  url: "get-ajax-images.php",
  data: "mode=ajax&start=10&end=20",

  success: function() { /* ... */ },
  error: function() { /* ... */ }
});

2.) PHP:根据startend

从数据库加载数据
<?php

header("Content-type: application/json");

// Please use intval() to avoid SQL injections!!
$start = isset($_GET['start']) ? intval($_GET['start']) : exit('{"error": true}');
$end = isset($_GET['end']) ? intval($_GET['end']) : exit('{"error": true}');

/* valid start and end? */
if ( $start < 0 || $end < 0 )
  exit('{"error": true}');

if ( ($end - $start) <= 0 ||
     ($end - $start) > 15 )
  exit('{"error": true}');


/* All right! */

$sql = "SELECT id, imagepath FROM images LIMIT $start, $end";
$result = mysql_query($sql) or exit('{"error": true}');

$data = array();
while ( $row = mysql_fetch_assoc($result) )
{
  $data[] = $row;
}
echo json_encode($data);

关于PHP通过JQuery请求MYSQL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8150383/

相关文章:

php - 重写代码以使用 PDO,并提高多次插入的效率

php - 如何在没有 access_token 的情况下显示来 self 网站的 facebook 提要消息?

jquery - 如何随时间显示和隐藏 div 并单击隐藏选项?

php - Laravel 中缓慢的 MySQL 查询在其他地方却很快

mysql - 时差rails(其中DATE mysql)

MySql:分组依据选择特定值或最大值

php - YII 中的 CHttpException 异常

php - preg_match :print: class matches tab character

javascript - 如何通过 ulr load 删除 jQuery UI 对话框上的内容?

javascript - 用于更新 mysql 中字段的 Jtable.org 自定义按钮 onclick