Reputation: 4527
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":[]}
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?
Upvotes: 0
Views: 131
Reputation: 522195
You are mysql_real_escape_string
ing your parameters before establishing a connection to the database. mysql_real_escape_string
needs an existing connection to the database to do its job. If there is none, it'll try to establish a connection by itself, which most likely fails, which means your parameters are 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.
Upvotes: 1