简体   繁体   English

在PHP中使用重音符号处理JSON对象

[英]Handling JSON objects with accents in PHP

I'm trying to create a json object from MySQL results, but not getting the result I need. 我正在尝试从MySQL结果创建一个json对象,但没有得到我需要的结果。 I don't know where is the problem. 我不知道问题出在哪里。 I execute on localhost and there is nothing (white windows). 我在本地主机上执行,没有任何内容(白色窗口)。

I want something like this: 我想要这样的东西:

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

//-------------My code bellow this line-------------------------- // -------------我的代码在这行下面--------------------------

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

This is my real code: 这是我的真实代码:

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

This is my SQL export code 这是我的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 */;

You need convert your characters with utf8_encode. 您需要使用utf8_encode转换字符。 Change your code to: 将您的代码更改为:

    $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"];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM