【发布时间】:2021-08-13 21:25:49
【问题描述】:
我最近升级到 PHP 8.09,发现我的 jqGrids 无法正常工作。他们在所有以前版本的 PHP 上工作。经过一番调查,我发现演示代码使用的 PHP 类对象 $responce 不存在或没有被实例化。代码失败并显示“致命错误:未捕获的错误:尝试在 null 上分配属性“页面””
该代码基于 trirand 服务器演示代码,用于从 MySQL 服务器加载 JSON 数据。
请参阅https://www.php.net/manual/en/migration80.incompatible.php上的“许多警告已转换为错误异常”
试图写入非对象的属性。以前,这为 null、false 和空字符串隐式创建了一个 stdClass 对象。 试图访问未定义的非限定常量。以前,不合格的常量访问会导致警告并被解释为字符串。
所有这些都意味着 trirand 上的 jqGrid 演示代码在使用 PHP 8+ 时会失败。似乎没有 php.ini 设置来关闭这些异常。请让我知道解决此问题的最佳方法是什么。如果您可以提供一些使用 $responce 作为数组的代码,那会有所帮助。
谢谢
更新:
为了解决这个问题,我创建了一个示例,将 $responce 对象设置为数组变量。
jqGrid 服务器端 PHP 与 MySQL
...
$page = $_GET['page']; // get the requested page
$limit = $_GET['rows']; // get how many rows we want to have into the grid
$sidx = $_GET['sidx']; // get index row - i.e. user click to sort
$sord = $_GET['sord']; // get the direction
if(!$sidx) $sidx =1;
// connect to the database
$db = mysql_connect($dbhost, $dbuser, $dbpassword)
or die("Connection Error: " . mysql_error());
mysql_select_db($database) or die("Error conecting to db.");
$result = mysql_query("SELECT COUNT(*) AS count FROM invheader a, clients b WHERE a.client_id=b.client_id");
$row = mysql_fetch_array($result,MYSQL_ASSOC);
$count = $row['count'];
if( $count >0 ) {
$total_pages = ceil($count/$limit);
} else {
$total_pages = 0;
}
if ($page > $total_pages) $page=$total_pages;
$start = $limit*$page - $limit; // do not put $limit*($page - 1)
$SQL = "SELECT a.id, a.invdate, b.name, a.amount,a.tax,a.total,a.note FROM invheader a, clients b WHERE a.client_id=b.client_id ORDER BY $sidx $sord LIMIT $start , $limit";
$result = mysql_query( $SQL ) or die("Couldn t execute query.".mysql_error());
$responce=array();
$responce['page'] = $page;
$responce['total'] = $total_pages;
$responce['records'] = $count;
$i=0;
while($row = mysql_fetch_array($result,MYSQL_ASSOC)) {
$responce['rows'][$i]['id']=$row['id'];
$responce['rows'][$i]['cell']=array($row['id'],date('m/d/Y',strtotime($row['invdate'])),$row['name'],$row['amount'],$row['tax'],$row['total'],$row['note']);
$i++;
}
echo json_encode($responce);
...
【问题讨论】: