Imr An Imr An - 8 months ago 24
MySQL Question

Data is not retrieving from Mysql database

This is my index.php file

I am not getting any data from Mysql database. Page is loading empty, it is not loading or fetching any results from database. Please help me. Thanks

<!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>


And this is my get_data.php file

<?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>';
}
}




?>


This is My database structure

-- 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

Answer Source

Try mysqli error function here is:

<?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>';
    }
}

?>

This gives you a error if there is error in you sql query.