这是我班上的代码
<?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;
}
}
?>
我正在尝试获取如下代码所示的输出
<?
$details = $PerClass->getOrders('1');
print_r($details);
?>
但是不幸的是,我正在追随错误
致命错误:在第37行的/home/domn/public_html/stage/stage_test.php中的null上调用成员函数getOrders()
尝试了不同的方法,但我认为我做错了事
答案 0 :(得分:0)
调用getOrders
方法的代码缺少对象实例化。
<?
// add this here
$PerClass = new PerClass();
$details = $PerClass->getOrders('1');
print_r($details);
?>
现在,由于您的PerClass
的构造方法希望您传入一个值作为参数,因此将导致以下警告:
警告PerClass :: __ construct()缺少参数1
为了解决此警告,您有两种选择:
$env
参数的值,即$PerClass = new PerClass('value_to_be_passed');
或$env
自变量的构造函数,因为-从我所看到的-它没有在任何地方使用,即从function __construct($env) { ... }
到function __construct() { ... }
。有关在PHP中使用global
的有趣讨论,请参见this link。