[英]Something is wrong with my code and i don't know what,
i'm trying to figure this out for days and i can't find what the error might be. 我试图解决这几天,我找不到错误可能是什么。
This is my code: 这是我的代码:
<?php
$page = isset($_POST['page']) ? intval($_POST['page']) : 1;
$rows = isset($_POST['rows']) ? intval($_POST['rows']) : 10;
include 'conn.php';
class get_data extends connection{
public function __construct($page,$rows){
$s_page=mysql_real_escape_string($page);
$s_rows=mysql_real_escape_string($rows);
$this->get_all($s_page,$s_rows);
}
public function get_all($page,$rows){
$this->connect_to_db();
$tablename = $this->define_table_name();
$offset = ($page-1)*$rows;
$result = array();
$rs = mysql_query("select count(*) from $tablename");
$row = mysql_fetch_row($rs);
$result["total"] = $row[0];
$startq="select * from $tablename limit $offset,$rows";
$rs = mysql_query("select * from $tablename limit $offset,$rows");
$items = array();
while($row = mysql_fetch_object($rs)){
array_push($items, $row);
}
$result["rows"] = $items;
echo json_encode($result);
}
}
$getdata=new get_data($page,$rows);
?>
The Output is {"total":"3","rows":[]}
输出为{"total":"3","rows":[]}
THE PROBLEM IS: the rows are empty but it counts how many rows are in the db meaning that the connection is good but the query for the row is not working any suggestions? 问题是:行为空,但它计算数据库中有多少行,这意味着连接良好,但对行的查询没有任何建议?
You are mysql_real_escape_string
ing your parameters before establishing a connection to the database. 在建立与数据库的连接之前,您正在mysql_real_escape_string
参数。 mysql_real_escape_string
needs an existing connection to the database to do its job. mysql_real_escape_string
需要与数据库的现有连接才能完成其工作。 If there is none, it'll try to establish a connection by itself, which most likely fails, which means your parameters are null
. 如果不存在,它将尝试自行建立连接,这很可能失败,这意味着您的参数为null
。
That should've been easy to figure out if you'd've enabled error reporting or would have debugged your app by simply var_dump
ing various variables here and there. 如果您已经启用了错误报告功能,或者只是通过var_dump
到处乱码各种变量来调试应用程序,那应该很容易弄清楚。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.