duanfu4446 2019-02-13 03:13
浏览 101

解析JSON文件时出错 - 不正确的对象

I've replicated a graph script from one Wordpress installation to another

It operates using graph_nat and defs.php - Defs stores the DB details

I have not altered the script after migrating but now I'm getting JSON error

I've checked to ensure after object it's true

I'm struggling to figure out the bug, error reporting doesn't include the JSON error only false positives for 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 );
?>

</div>
  • 写回答

1条回答 默认 最新

  • dongpan3001 2019-02-13 03:31
    关注

    Figured it out, I wasn't passing USER ID in url. It was undefined. I should go back to school

    评论

报告相同问题?

悬赏问题

  • ¥15 chaquopy python 安卓
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 有没有帮写代码做实验仿真的
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥30 vmware exsi重置后登不上
  • ¥15 易盾点选的cb参数怎么解啊
  • ¥15 MATLAB运行显示错误,如何解决?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题