doujin8673 2013-07-10 23:45
浏览 37

PHP简单的功能不起作用

I can't make a simple echo.

I have an admin.class.php

public static function get_quota() {
  return self::find_by_sql("SELECT * FROM quota");
}

public static function find_by_sql($sql="") { 
  global $database; 
  $result_set = $database->query($sql); 
  $object_array = array(); 
  while ($row = $database->fetch_array($result_set)) { 
    $object_array[] = self::instantiate($row); 
  } 
  return $object_array; 
}

And my echo code in index.php

<?php
    $admin = User::find_by_id($_SESSION['user_id']);
    $admin_class = new Admin();
    $get_quota = Admin::get_quota();
    $sql = "SELECT * FROM quota";
    $get_quota = Admin::find_by_sql($sql);
?>
.
.
.
<?php echo $get_quota->daily_a; ?>

So my problem is, that the code is not working. I cannot echo my data. Can you help me, please?

  • 写回答

1条回答 默认 最新

  • douzhu7507 2013-07-10 23:49
    关注

    You have a couple of problems here:

    <?php echo $get_quota->daily_a; ?>
    

    This line references the $get_quota variable and searches for a member field daily_a. Try this:

    <?php echo "Quota is:".var_export($get_quota->daily_a,true); ?>
    

    This will show that that is simply an empty variable.

    However, also note:

    $get_quota = Admin::get_quota();
    $sql = "SELECT * FROM quota";
    $get_quota = Admin::find_by_sql($sql);
    

    Here you are calling two separate methods from Admin and setting the variable $get_quota to the result. The second overwrites the first. Therefore the get_quota() method doesn't help us here: we need to know what your find_by_sql() method returns.

    EDIT (Post new code added to question)

    You can implement logging/echoing within the function you've a problem with:

    public static function find_by_sql($sql="") { 
      global $database; //Note this is bad practice (1).
      $result_set = $database->query($sql); 
      echo "Result Set: ".var_export($result_set,true)."
    ";//This should return something if you're getting something back from the database. Also, remove this for runtime (obviously).
    
      if (count($result_set) <= 0) { //constraint checking is always good! And cheap!
        error_log("An unexpected number of rows (0) was received from the database for query='".$sql."'.");
      }
    
      $object_array = array(); 
      while ($row = $database->fetch_array($result_set)) { 
        $object_array[] = self::instantiate($row); //Ensure that the instantiate function returns something!
      } 
    
      echo "Object Array: ".var_export($object_array, true)."
    ";//double-check your instantiate function is working
      return $object_array; 
    }
    

    Based on this code, your problem is likely with the instantiate function; if it's not returning anything, $object_array is probably empty. (But not null!).

    (1) You should avoid grabbing global variables like this. Instead, instantiate a class that holds and manages your database connection. Then make your find_by_sql function non-static and have a member field pointing to your database.

    评论

报告相同问题?

悬赏问题

  • ¥17 pro*C预编译“闪回查询”报错SCN不能识别
  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向