doulu2591 2018-09-01 21:54
浏览 127
已采纳

无法从PHP类获得返回输出

This is my code in my class

    <?php
class PerClass
{
    private $sql_connection = null;
    private $localAf = '9929292F';
    function __construct($env) {
        // Nasty globals, sorry
        global $_config;
    $host = "localhost";
    $user = "user";
    $pass = "pass";
    $db = "kModule";


        // Build sql connection
        $this->sql_connection = new mysqli($host, $user, $pass, $db);
        // Check connection
        if ($this->sql_connection->connect_error) {
            die("Connection failed: " . $this->sql_connection->connect_error);
        }
    }

    public function getOrders($sSettingsId) {
        $query = <<<SQL
            SELECT * FROM `scrub_order_log` WHERE `scrub_settings_id` = {$sSettingsId} AND `order_date` BETWEEN (NOW() - INTERVAL (SELECT `c_h_days` FROM `scrub_settings` WHERE `id` = {$sSettingsId}) DAY) AND NOW() ORDER BY `order_date` DESC;
SQL;
        $result = $this->sql_connection->query($query);
        $resp = null;
        while ($row = $result->fetch_assoc()) {
            $resp[] = $row;
        }
        return $resp;
    }
}
?>

I am trying to get the output as shown in code below

<?
$details = $PerClass->getOrders('1');
print_r($details);
?>

But unfortunately I am getting following erro

Fatal error: Call to a member function getOrders() on null in /home/domn/public_html/stage/stage_test.php on line 37

Tried different ways but I think I am doing something wrong

  • 写回答

1条回答 默认 最新

  • dt3999 2018-09-01 22:20
    关注

    The code that calls the getOrders method is missing the object instantiation.

    <?
        // add this here
        $PerClass = new PerClass();
        $details = $PerClass->getOrders('1');
        print_r($details);
    ?>
    

    now, because the constructor method of your PerClass expects you to pass in a value as an argument, this is going to result in the following warning:

    WARNING Missing argument 1 for PerClass::__construct()

    In order to resolve this warning you have two options:

    1. Pass the value of the $env parameter when you instantiate the object i.e. $PerClass = new PerClass('value_to_be_passed'); or
    2. Get rid of the $env argument in your constructor since - from what I can see - it is not used anywhere i.e. from function __construct($env) { ... } to function __construct() { ... }.

    See this link for an interested discussion about using global in PHP.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 在获取boss直聘的聊天的时候只能获取到前40条聊天数据
  • ¥20 关于URL获取的参数,无法执行二选一查询
  • ¥15 液位控制,当液位超过高限时常开触点59闭合,直到液位低于低限时,断开
  • ¥15 marlin编译错误,如何解决?
  • ¥15 有偿四位数,节约算法和扫描算法
  • ¥15 VUE项目怎么运行,系统打不开
  • ¥50 pointpillars等目标检测算法怎么融合注意力机制
  • ¥20 Vs code Mac系统 PHP Debug调试环境配置
  • ¥60 大一项目课,微信小程序
  • ¥15 求视频摘要youtube和ovp数据集