0

So I was as normal fetching results from my MYSQL database using PDO from a view table I created, all is fine with var_dump and print_r UNTIL I try to set the result to any array key.

public function getBag($user = false)
{
    $id = ($user) ? $user['id'] : $_SESSION['user']['id'];

    $bag = $this->prepareFromArray('bag', array('user_id' => $id), 'select', 'all', array('user_id'));

    $result = (isset($bag[0]['id'])) ? $bag : null;

    return $result;
}

public function prepPlayer($user)
{
    $user['medals'] = $this->getmedals($user);
    $user['pokemons'] = $this->getPokemons($user);
    $user['bag'] = $this->getBag($user);

    for ($i=0; $i < 6; $i++) { 
        $user['pokemons']['roster'][$i] = (isset($user['pokemons']['roster'][$i])) ? $user['pokemons']['roster'][$i] : ['name' => "None", "level" => 0, "wild_id" => 0, "color" => 0];
    }

    $trainerClass = strtolower(trim($user['class']));

    $cover = "img/Trainers/$trainerClass/background/{$user['wallpaper']}.png";

    $user['cover'] = (file_exists($_SERVER['DOCUMENT_ROOT']."/$cover")) ? $cover : "img/Trainers/backgrounds/1.png";

    return $user;
}

I am using the prepPlayer method to prepare players when called upon, everything works fine UNTIL we get to getting the players bag.

if I comment out the db (prepareFromArray) method it will naturaly work, if I print_r the bag variable, it shows all items from the bag view, but once I try to return the result into the $user['bag'] array element all hell breaks lose, and the result is a blank page for my stream file that depends on this. I am using

echo json_encode(array('result' => $user), JSON_NUMERIC_CHECK);

to output the result to my angular service.

here is how my bag view looks like

CREATE VIEW `bag` AS
SELECT 
    `player_items`.`id` AS `id`,
    `players`.`id` AS `user_id`,
    `items`.`id` AS `item`,
    `items`.`qual` AS `qual`,
    `items`.`cat` AS `cat`,
    `items`.`name` AS `name`,
    `items`.`description` AS `description`,
    `items`.`chance` AS `chance`,
    `items`.`cost` AS `cost`,
    `items`.`trade` AS `trade`,
    `player_items`.`amount` AS `amount`,
    `player_items`.`cap` AS `cap`,
    `player_items`.`sell` AS `sell`,
    `items`.`temp` AS `temp`
FROM
    ((`players`
    JOIN `items`)
    JOIN `player_items`)
WHERE
    ((`players`.`id` = `player_items`.`user_id`)
        AND (`player_items`.`item_id` = `items`.`id`))
ORDER BY `player_items`.`id`
3
  • Is it ok that you use $user as a parameter and rewriting it in your prepPlauer function? Commented May 27, 2015 at 19:51
  • Using it as a parameter gives me access to it as if I declared it in the code block, although In this case $userClass->getBag() I can call without prepPlayer and it would still break. Commented May 27, 2015 at 20:39
  • After looking what you mean @u_mulder they $user passed in to the methods, are only used to find out any user info I need, what is returned form the methods are array objects of what I specify. Commented May 27, 2015 at 20:43

1 Answer 1

0

To anyone that ever has this issue, you need to first check UTF8 errors of a JSON encode call, like so.

from: How to solve JSON_ERROR_UTF8 error in php json_decode?

function safe_json_encode($value){
    if (version_compare(PHP_VERSION, '5.4.0') >= 0) {
        $encoded = json_encode($value, JSON_NUMERIC_CHECK, JSON_PRETTY_PRINT);
    } else {
        $encoded = json_encode($value, JSON_NUMERIC_CHECK);
    }
    switch (json_last_error()) {
        case JSON_ERROR_NONE:
            return $encoded;
        case JSON_ERROR_DEPTH:
            return 'Maximum stack depth exceeded'; // or trigger_error() or throw new Exception()
        case JSON_ERROR_STATE_MISMATCH:
            return 'Underflow or the modes mismatch'; // or trigger_error() or throw new Exception()
        case JSON_ERROR_CTRL_CHAR:
            return 'Unexpected control character found';
        case JSON_ERROR_SYNTAX:
            return 'Syntax error, malformed JSON'; // or trigger_error() or throw new Exception()
        case JSON_ERROR_UTF8:
            $clean = utf8ize($value);
            return safe_json_encode($clean);
        default:
            return 'Unknown error'; // or trigger_error() or throw new Exception()

    }
}

function utf8ize($mixed) {
    if (is_array($mixed)) {
        foreach ($mixed as $key => $value) {
            $mixed[$key] = utf8ize($value);
        }
    } else if (is_string ($mixed)) {
        return utf8_encode($mixed);
    }
    return $mixed;
}

If you don't do this, you will receive a blank page when ever the array you are encoding a utf8 error.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.