【问题标题】:jqGrid: PHP 8+ Breaking Demos and code based on DemosjqGrid: PHP 8+ Breaking Demos和基于Demos的代码
【发布时间】: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);
...

【问题讨论】:

    标签: php jqgrid


    【解决方案1】:

    demo我没看,不过好像解决办法是启动变量。

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

    $response = new stdClass();
    ...
    

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

    【讨论】:

    • 您好,请参阅第二个 PHP 8+ 向后不兼容性“尝试访问未定义的非限定常量。”正如所写,即使您使用 $responce = new stdClass(); 实例化变量,演示也会失败。您必须创建一个 PHP 类并包含在构造函数中定义的所有属性。这就是为什么我建议将 $responce 更改为数组示例的原因。谢谢
    猜你喜欢
    • 1970-01-01
    • 2018-07-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-08
    • 2011-11-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多