我的代码出了点问题,我也不知道, [英] Something is wrong with my code and i don't know what,

查看:64
本文介绍了我的代码出了点问题,我也不知道,的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图解决这一问题几天,但我找不到可能的错误.

i'm trying to figure this out for days and i can't find what the error might be.

这是我的代码:

<?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);
 ?>

输出为{"total":"3","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?

推荐答案

您正在mysql_real_escape_string在建立与数据库的连接之前 设置参数. mysql_real_escape_string需要与数据库的现有连接才能执行其工作.如果不存在,它将尝试自行建立连接,这很可能失败,这意味着您的参数为null.

You are mysql_real_escape_stringing 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.

如果您已经启用了错误报告功能,或者只是通过var_dump到处乱码各种变量来调试应用程序,那应该很容易弄清楚.

That should've been easy to figure out if you'd've enabled error reporting or would have debugged your app by simply var_dumping various variables here and there.

这篇关于我的代码出了点问题,我也不知道,的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆