问题描述
我已将一个图形脚本从一个Wordpress安装复制到另一个
它使用graph_nat和defs.php进行操作-Defs存储数据库详细信息
迁移后我没有更改脚本,但是现在出现JSON错误
我已检查以确保事后真实
我正在努力找出错误,错误报告不包括JSON错误,仅包含PHP的误报
<?php include ('../wp-load.php'); include ('defs.php'); // we need this so that PHP does not complain about deprectaed functions error_reporting( 0 ); // Connect to MySQL // constants stored in defs.php $db = mysqli_connect("localhost", DB_NAT_USER, DB_NAT_PASS, DB_NAT_NAME); // get user id $current_user = wp_get_current_user(); $current_user_id = $current_user->ID; if ( $current_user_id == null || $current_user_id == 0) { $message = 'User not authorized'; die( $message ); } if ( !$db ) { die( 'Could not connect to database' ); } if (!isset($_GET['id'])) { $message = 'Missing ID url parameter'; die( $message ); } $id = $_GET['id']; $practitionerId = $current_user_id; $query = "SELECT results FROM submissions WHERE ID = ? AND practitionerId = ?"; $result = []; if ($stmt = $db->prepare($query)) { $stmt->bind_param('ss', $id, $practitionerId); $stmt->execute(); $stmt->bind_result($results); if ($stmt->fetch()) { $result = $results; } $stmt->close(); } // decode json from database $json = json_decode($result, true); $outputArray = []; $healthIndex = 100; if ($json) { foreach($json as $key=>$val) { $healthEvents = explode(", ", $val); // filter out empty strings $healthEventsFiltered = array_filter($healthEvents, function($value) { if ($value == '') { return false; } return true; }); // points to decrease per event $healthDecrease = (count($healthEventsFiltered))*2; $healthIndex -= $healthDecrease; if ($healthIndex<0) { $healthIndex = 0; } // implode array to get description string $arrayString = implode(",<br>", $healthEventsFiltered); // age groups $ageGroup = $key*5; $ar = array("category" => "Age: " . $ageGroup, "column-1" => $healthIndex, "events" => $arrayString); array_push($outputArray, $ar); } echo json_encode($outputArray, true); } else { $message = 'Could not decode JSON: ' . $result; die( $message ); } // Close the connection mysqli_close( $db ); ?>
1楼
想通了,我没有在url中传递USER ID。 它是未定义的。 我应该回学校