Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/272.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 未从Mysql数据库检索数据_Php_Mysql_Ajax_Database - Fatal编程技术网

Php 未从Mysql数据库检索数据

Php 未从Mysql数据库检索数据,php,mysql,ajax,database,Php,Mysql,Ajax,Database,这是我的index.php文件 <?php if(isset($_GET['offset']) && isset($_GET['limit'])){ $limit = $_GET['limit']; $offset = $_GET['offset']; $connection = mysqli_connect('localhost', 'root', '', 'text'); $data

这是我的index.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 */;
我没有从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>


试试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查询中有错误,则会出现错误。

您的输出为空,因为没有向GET_data.php发送GET参数。试试这个:

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

无论何时编码,都要尽可能捕获所有异常

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

    });

您是否尝试过从浏览器中点击url。在浏览器中打开URL
http://localhost/... /获取_data.php?offset=10&limit=0
并检查输出是否符合要求。这是为了检查您的php代码是否有错误,请添加
ini\u集('display\u errors',1);ini设置(“显示启动错误”,1);错误报告(E_全部)
在两个文件的顶部尝试查看URL中的apache错误日志Ajax get request pass参数注释不用于扩展讨论;这段对话已经结束。