gpt4 book ai didi

php - 在 PHP 中处理带有重音符号的 JSON 对象

转载 作者:行者123 更新时间:2023-11-29 00:02:04 24 4
gpt4 key购买 nike

我试图从 MySQL 结果创建一个 json 对象,但没有得到我需要的结果。我不知道问题出在哪里。我在本地主机上执行,但什么也没有(白色窗口)。

我想要这样的东西:

{
arraytwo:{ 0:{pid:"..",name:"..",year:"..",subject:".."},
1:{pid:"..",name:"..",year:"",subject:".."},
2:{pid:"..",name:"..",year:"..",subject:".."},
...},
success:1
}

//------------我的代码在这一行下面------------------------

$response = array(); //array one (multi.)
$con = mysqli_connect(DB_HOST,DB_USER,DB_PASSWORD,DB_DATABASE);
$result = mysqli_query($con, "SELECT * FROM classroom") or die(mysqli_error());

if (mysqli_num_rows($result) > 0) {

$response["arraytwo"] = array(); //array two

while ($row = mysqli_fetch_array($result,MYSQLI_BOTH)) {

$arraythree = array(); //array three

$arraythree["pid"] = $row["pid"];
$arraythree["name"] = $row["name"];
$arraythree["year"] = $row["year"];
$arraythree["subject"] = $row["subject"];

array_push($response["arraytwo"], $arraythree);
}

$response["success"] = 1;

echo json_encode($response);
}

这是我的真实代码:

<?php

$response = array();

$con = mysqli_connect(DB_HOST,DB_USER,DB_PASSWORD,DB_DATABASE);

$result = mysqli_query($con, "SELECT *FROM products") or die(mysqli_error());

if (mysqli_num_rows($result) > 0) {

$response["noticias"] = array();

while ($row = mysqli_fetch_array($result,MYSQLI_BOTH)) {

$noticia = array();

$noticia["pid"] = $row["pid"];
$noticia["nombre"] = $row["nombre"];
$noticia["curso"] = $row["curso"];
$noticia["asignatura"] = $row["asignatura"];
$noticia["accion"] = $row["accion"];
$noticia["descripcion"] = $row["descripcion"];
$noticia["created_at"] = $row["created_at"];
$noticia["updated_at"] = $row["updated_at"];


array_push($response["noticias"], $noticia);

}
echo json_encode($response);
$response["success"] = 1;
echo json_encode($response);

} else {

$response["success"] = 0;
$response["message"] = "No notices found";
echo json_encode($response);
}
?>

这是我的 SQL 导出代码

-- phpMyAdmin SQL Dump
-- version 4.1.14
-- http://www.phpmyadmin.net
--
-- Servidor: 127.0.0.1
-- Tiempo de generación: 28-03-2015 a las 21:12:02
-- Versión del servidor: 5.6.17
-- Versión de PHP: 5.5.12

SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
SET time_zone = "+00:00";


/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8 */;

--
-- Base de datos: `pfc`
--

-- --------------------------------------------------------

--
-- Estructura de tabla para la tabla `products`
--

CREATE TABLE IF NOT EXISTS `products` (
`pid` int(11) NOT NULL AUTO_INCREMENT,
`nombre` varchar(100) NOT NULL,
`curso` varchar(100) NOT NULL,
`asignatura` varchar(100) NOT NULL,
`accion` varchar(10) NOT NULL,
`contacto` varchar(30) NOT NULL,
`descripcion` text,
`created_at` timestamp NULL DEFAULT CURRENT_TIMESTAMP,
`updated_at` timestamp NULL DEFAULT NULL,
PRIMARY KEY (`pid`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=5 ;

--
-- Volcado de datos para la tabla `products`
--

INSERT INTO `products` (`pid`, `nombre`, `curso`, `asignatura`, `accion`, `contacto`, `descripcion`, `created_at`, `updated_at`) VALUES
(1, 'Salva', '2bach', 'matematicas', 'donar', 'telSalva', 'libro precioso con problemas sin resolver', '2015-03-28 04:42:44', NULL),
(2, 'Pedro Sánchez', '1Universidad', 'Física cuántica', 'Alquiler', 'telefono2828', 'Libro a estrenar de formulas de toda la carrera', '2015-03-28 04:44:12', NULL),
(3, 'marcos', 'cuarto de primaria', 'abecedario', 'vender', 'telefonomolón', 'letras de cartón para vender', '2015-03-28 17:16:05', NULL);

/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;

最佳答案

您需要使用 utf8_encode 转换您的字符。将您的代码更改为:

    $noticia["pid"] = $row["pid"];
$noticia["nombre"] = utf8_encode($row["nombre"]);
$noticia["curso"] = $row["curso"];
$noticia["asignatura"] = utf8_encode($row["asignatura"]);
$noticia["accion"] = $row["accion"];
$noticia["descripcion"] = utf8_encode($row["descripcion"]);
$noticia["created_at"] = $row["created_at"];
$noticia["updated_at"] = $row["updated_at"];

关于php - 在 PHP 中处理带有重音符号的 JSON 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29321487/

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