gpt4 book ai didi

从 mysql 查看结果设置类数组键时 PHP 中断

转载 作者:行者123 更新时间:2023-11-29 22:23:30 24 4
gpt4 key购买 nike

所以我像平常一样使用我创建的 View 表中的 PDO 从 MYSQL 数据库中获取结果,使用 var_dump 和 print_r 一切都很好,直到我尝试将结果设置为任何数组键。

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;
}

我正在使用 prepPlayer 方法在需要时准备球员,一切正常,直到我们拿到球员包为止。

如果我注释掉 db (prepareFromArray) 方法,它自然会工作,如果我 print_r bag 变量,它会显示 bag View 中的所有项目,但是一旦我尝试将结果返回到 $user['bag' ] 数组元素全部丢失,结果是我的流文件的空白页面取决于此。我正在使用

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

将结果输出到我的角度服务。

这是我的包 View 的样子

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`

最佳答案

对于遇到此问题的任何人,您需要首先检查 JSON 编码调用的 UTF8 错误,如下所示。

来自: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;
}

如果您不这样做,当您编码的数组出现 utf8 错误时,您将收到一个空白页。

关于从 mysql 查看结果设置类数组键时 PHP 中断,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30491776/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com