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 ssh登录页面的问题
  • ¥60 渗透一个指定银行app,拿到客户信息,需要什么级别
  • ¥50 关于在matlab上对曲柄摇杆机构上一点的运动学仿真
  • ¥15 jetson nano
  • ¥15 :app:debugCompileClasspath'.
  • ¥15 windows c++内嵌qt出现数据转换问题。
  • ¥20 公众号如何实现点击超链接后自动发送文字
  • ¥15 用php隐藏类名和增加类名
  • ¥15 算法设计与分析课程的提问
  • ¥15 用MATLAB汇总拟合图