我是脚本新手,我尝试在教程后用javascript分页数据库结果,但是不起作用,它给出了一些错误,我按照所有步骤,如果我在脚本,但我没有找到任何东西..我会把错误
警告:mysqli_query():无法在第6行 C:.. \ localweb \ test-pagini.php中获取mysqli
警告:mysqli_fetch_row()要求参数1为mysqli_result ,在第7行的C:.. \ localweb \ test-pagini.php中给出null
警告:mysqli_close():无法在第19行 C:... \ localweb \ test-pagini.php中获取mysqli
这是代码
//名为mysqli_connection.php的连接
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "db";
//create connection to mysql
$db_conx = mysqli_connect($servername, $username, $password, $dbname);
if(!$db_conx){
die("Connection failed: ". mysqli_connect_error());
}
// create table
$users = "CREATE TABLE IF NOT EXISTS users(
id INT(11) NOT NULL AUTO_INCREMENT,
titlu VARCHAR(50) NOT NULL,
descriere VARCHAR(50) NOT NULL,
data DATE NOT NULL,
approved ENUM('0','1') DEFAULT '1',
PRIMARY KEY (id)
)";
//confirm success or fail
$query = mysqli_query($db_conx, $users);
if(mysqli_query($db_conx, $users)){
echo "<h2>Success user table</h2>";
}else{
echo "<h2>Fail user table</h2>";
echo mysqli_error($db_conx);
}
// Close the database connection
mysqli_close($db_conx);
?>
//现在是test-pagini.php
<?php
// Connect to our database here
include_once("/mysqli_connection.php");
// This first query is just to get the total count of rows
$sql = "SELECT COUNT(id) FROM users WHERE approved='1'";
$query = mysqli_query($db_conx, $sql);
$row = mysqli_fetch_row($query);
// Here we have the total row count
$total_rows = $row[0];
// Specify how many results per page
$rpp = 10;
// This tells us the page number of our last page
$last = ceil($total_rows/$rpp);
// This makes sure $last cannot be less than 1
if($last < 1){
$last = 1;
}
// Close the database connection
mysqli_close($db_conx);
?>
<!DOCTYPE html>
<html>
<head>
<script>
var rpp = <?php echo $rpp; ?>; // results per page
var last = <?php echo $last; ?>; // last page number
function request_page(pn){
var results_box = document.getElementById("results_box");
var pagination_controls = document.getElementById("pagination_controls");
results_box.innerHTML = "loading results ...";
var hr = new XMLHttpRequest();
hr.open("POST", "/pagini_plugin.php", true);
hr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
hr.onreadystatechange = function() {
if(hr.readyState == 4 && hr.status == 200) {
var dataArray = hr.responseText.split("||");
var html_output = "";
for(i = 0; i < dataArray.length - 1; i++){
var itemArray = dataArray[i].split("|");
html_output += "ID: "+itemArray[0]+" - Testimonial from <b>"+itemArray[1]+"</b><hr>";
}
results_box.innerHTML = html_output;
}
}
hr.send("rpp="+rpp+"&last="+last+"&pn="+pn);
// Change the pagination controls
var paginationCtrls = "";
// Only if there is more than 1 page worth of results give the user pagination controls
if(last != 1){
if (pn > 1) {
paginationCtrls += '<button onclick="request_page('+(pn-1)+')"><</button>';
}
paginationCtrls += ' <b>Page '+pn+' of '+last+'</b> ';
if (pn != last) {
paginationCtrls += '<button onclick="request_page('+(pn+1)+')">></button>';
}
}
pagination_controls.innerHTML = paginationCtrls;
}
</script>
</head>
<body>
<div id="pagination_controls"></div>
<div id="results_box"></div>
<script> request_page(1); </script>
</body>
</html>
//和pagini_plugin.php
<?php
// Make the script run only if there is a page number posted to this script
if(isset($_POST['pn'])){
$rpp = preg_replace('#[^0-9]#', '', $_POST['rpp']);
$last = preg_replace('#[^0-9]#', '', $_POST['last']);
$pn = preg_replace('#[^0-9]#', '', $_POST['pn']);
// This makes sure the page number isn't below 1, or more than our $last page
if ($pn < 1) {
$pn = 1;
} else if ($pn > $last) {
$pn = $last;
}
// Connect to our database here
include_once("mysqli_connection.php");
// This sets the range of rows to query for the chosen $pn
$limit = 'LIMIT ' .($pn - 1) * $rpp .',' .$rpp;
// This is your query again, it is for grabbing just one page worth of rows by applying $limit
$sql = "SELECT id, titlu, descriere, data FROM testimonials WHERE approved='1' ORDER BY id DESC $limit";
$query = mysqli_query($db_conx, $sql);
$dataString = '';
while($row = mysqli_fetch_array($query, MYSQLI_ASSOC)){
$id = $row["id"];
$titlu = $row["titlu"];
$descriere = $row["descriere"];
$itemdate = strftime("%b %d, %Y", strtotime($row["data"]));
$dataString .= $id.'|'.$firstname.'|'.$lastname.'|'.$itemdate.'||';
}
// Close your database connection
mysqli_close($db_conx);
// Echo the results back to Ajax
echo $dataString;
exit();
}
?>
我使用了easyPhp v14.1和Windows7。
答案 0 :(得分:0)
您已快速关闭数据库连接:
mysqli_close($db_conx);
mysqli_connection.php
结束时包含在 test-pagini.php
中连接关闭,然后无法获取..
注意:
include_once("/mysqli_connection.php");
删除&#34; /&#34;,是正确的路径语法
注2:
在 pagini_plugin.php
的第一行if(isset($_POST['pn'])){
使用empty而不是isset,以避免空查询 该本机函数,避免与isset相同的行为(二合一) 看看差异:empty() VS isset()
if(!empty($_POST['pn'])){
编辑:
实际上你的迭代太多错误要继续留在评论框中.. 我认为问题得到了回答,而且还有更多。
寻找现代系统: 由于过时和错误的编码方法,您将节省大量时间,错误很多。
PHP, MYSQL, HTML table with tablesorter
http://tablesorter.com/docs/#Examples
(sry但是我没有更多的时间来帮助你,8小时的堆栈溢出,我已经睡了:))
答案 1 :(得分:0)
我认为这是因为你正在关闭文件末尾的mysqli_connection.php中的连接。
// Close the database connection
mysqli_close($db_conx);