数据不是从Mysql数据库中检索的

时间:2017-03-12 08:15:21

标签: php mysql ajax database

这是我的index.php文件

我没有从Mysql数据库中获取任何数据。页面加载为空,它不加载或从数据库中获取任何结果。请帮我。感谢

<!DOCTYPE html>
<html>
<head>

<link rel="stylesheet" type="text/css" href="sty.css" />
<link rel="stylesheet" type="text/css" href="css/font-awesome.css" />       
</head>
<body>
 <div id='data'></div>
</body>


<script src="code.jquery.com/jquery-3.1.1.js"; integrity="sha256-16cdPddA6VdVInumRGo6IbivbERE8p7CQR3HzTBuEL‌​A=" crossorigin="anonymous"></script>
<script type="text/javascript">
    $(document).ready(function(){


     $.ajax({

             type: "GET",
             url: "get_data.php",
             data: {
                 'offset':1,
                 'limit' :5
            },
            success: function(data){
                $('#data').html(data);
            }

       });

    });


</script>

</html>

这是我的get_data.php文件

<?php



    if(isset($_GET['offset']) && isset($_GET['limit'])){

        $limit = $_GET['limit'];
        $offset = $_GET['offset'];  

        $connection = mysqli_connect('localhost', 'root', '', 'text');

        $data = mysqli_query($connection, "SELECT * FROM `text` LIMIT {$limit} OFFSET {$offset}");
        while($row = mysqli_fetch_array($data)) {
          echo '<div id="post1"><p>'.$row['text'].'</p></div>';
         }
    }




?>

这是我的数据库结构

-- phpMyAdmin SQL Dump
-- version 4.5.1
-- http://www.phpmyadmin.net
--
-- Host: 127.0.0.1
-- Generation Time: Mar 12, 2017 at 09:08 AM
-- Server version: 10.1.19-MariaDB
-- PHP Version: 7.0.13

SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
SET time_zone = "+00:00";


/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8mb4 */;

--
-- Database: `text`
--

-- --------------------------------------------------------

--
-- Table structure for table `text`
--

CREATE TABLE `text` (
  `id` int(11) NOT NULL,
  `image` varchar(200) NOT NULL,
  `text` longtext NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

--
-- Dumping data for table `text`
--

INSERT INTO `text` (`id`, `image`, `text`) VALUES
(1, '', ''),
(2, '', 'hi'),
(3, '', 'hhh'),
(4, '', 'dgg'),
(5, '', 'hfg'),
(6, '', 'hfg'),
(7, '', 'hhhhh'),
(8, '', 'hi'),
(9, '', '1'),
(10, '', '2'),
(11, '', '3'),
(12, '', '4'),
(13, '', '5'),
(14, '', '4'),
(15, '', '1'),
(16, '', '1'),
(17, '', '3'),
(18, '', '121'),
(19, '', '45457'),
(20, '', 'hh'),
(21, '', '45'),
(22, '', 'fgsgf'),
(23, '', '45454545'),
(24, '', 'jj'),
(25, '', 'fafads'),
(26, '', 'asf'),
(27, '', 'fsdfsfg'),
(28, '', '1'),
(29, '', ''),
(30, '', ''),
(31, '', ''),
(32, '', ''),
(33, '', ''),
(34, '', ''),
(35, '', ''),
(36, '', ''),
(37, '', ''),
(38, '', '');

--
-- Indexes for dumped tables
--

--
-- Indexes for table `text`
--
ALTER TABLE `text`
  ADD PRIMARY KEY (`id`);

--
-- AUTO_INCREMENT for dumped tables
--

--
-- AUTO_INCREMENT for table `text`
--
ALTER TABLE `text`
  MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=39;
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;

index image

get data image

3 个答案:

答案 0 :(得分:1)

在这里试试mysqli错误函数是:

<?php

if(isset($_GET['offset']) && isset($_GET['limit'])){
$connection = mysqli_connect('localhost', 'root', '', 'text');
if(isset($_GET["limit"])){
 $limit = $_GET['limit'];
}
else{
 $limit=5;
}

if(isset($_GET["offset"])){
 $offset = $_GET['offset'];
}
else{
 $offset=1;
}

$data = mysqli_query($connection, "SELECT * FROM `text` LIMIT $limit OFFSET $offset")or die(mysqli_error($connection));


while($row = mysqli_fetch_array($data)) {
     echo '<div id="post1"><p>'.$row['text'].'</p></div>';
    }
}

?>

如果你的SQL查询中有错误,这会给你一个错误。

答案 1 :(得分:0)

你的空白输出导致没有GET参数被发送到get_data.php。试试这个:

url: "get_data.php?offset=foo&limit=bar",

答案 2 :(得分:0)

无论何时编码,都要尽量抓住所有可能的异常。

在您的ajax电话中,您没有处理失败案例,因此您无法在出错的地方找到它。

   $.ajax({
        type: "GET",
        url: "get_data.php",
        dataType: 'html',
        data: {
            'offset': 1,
            'limit': 5
        },
        success: function (data) {
            $('body').append(data);
        },
        error: function (jqXHR, exception) {
            var msg = '';
            if (jqXHR.status == 404) {
                msg = 'Requested page not found. [404]';
            } else if (jqXHR.status == 500) {
                msg = 'Internal Server Error [500].';
            } else if (exception === 'parsererror') {
                msg = 'Requested JSON parse failed.';
            } else if (exception === 'timeout') {
                msg = 'Time out error.';
            } else if (exception === 'abort') {
                msg = 'Ajax request aborted.';
            } else {
                msg = 'Uncaught Error.\n' + jqXHR.responseText;
            }
            alert(msg);
        }

    });