首页 > 解决方案 > jqGrid: PHP 8+ Breaking Demos和基于Demos的代码

问题描述

我最近升级到 PHP 8.09 并注意到我的 jqGrids 无法正常工作。他们在所有以前版本的 PHP 上工作。经过一番调查,我发现演示代码使用了一个不存在或尚未实例化的 PHP 类对象$responce 。代码因“致命错误:未捕获的错误:尝试在 null 上分配属性“页面”而失败

该代码基于用于从 MySQL 服务器加载 JSON 数据的 trirand 服务器演示代码代码。

请参阅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);
...

标签: phpjqgrid

解决方案


我没有检查演示,但似乎解决方案是启动变量。

为了解决这个问题,添加你的代码(在开始和分配任何属性之前的某处 -> 值到 $response 对象)

$response = new stdClass();
...

我会检查演示,如果有什么特别的地方会重播这篇文章


推荐阅读