Get arrays with space in php

1

I have a code in php that takes information from two different columns of the same table in a local database, but the code does not seem to work when one of the columns has information with space, for example: "Romeo Juliet" but is written: "Romeu_julieta" the code works, can anyone help me? Here is the code:

<?php 

$servername = "localhost";
$username = "root";
$password = "";
$dbname = "teste";

$conn = new mysqli ($servername,$username,$password,$dbname);

if ($conn->connect_error){
die("Connection failed: " . $conn->connect_error);
}
$sql1 = "SELECT abbreviation,image FROM je";

$result1 = $conn->query($sql1);
$i=0;
$resultado = array(array(),array());
while ($row = $result1->fetch_assoc()){
    $resultado[$i]["imagem"] = $row["image"];
    $resultado[$i]["abrevia"] = $row["abbreviation"];
    $i++;
}


echo json_encode($resultado);

?>
    
asked by anonymous 03.06.2016 / 01:04

1 answer

0

All entries in json_encode() The entire string must be encoded as UTF-8 . So to solve your problem, just format the input with utf8_encode :

<?php 

$servername = "localhost";
$username = "root";
$password = "";
$dbname = "teste";

$conn = new mysqli ($servername,$username,$password,$dbname);

if ($conn->connect_error){
die("Connection failed: " . $conn->connect_error);
}
$sql1 = "SELECT abbreviation,image FROM je";

$result1 = $conn->query($sql1);
$i=0;
$resultado = array(array(),array());
while ($row = $result1->fetch_assoc()){
    //$resultado[$i]["imagem"] = $row["image"];
    //$resultado[$i]["abrevia"] = $row["abbreviation"];
    $resultado[$i]["imagem"] = utf8_encode( $row["image"] );
    $resultado[$i]["abrevia"] = utf8_encode( $row["abbreviation"]);
    $i++;
}


echo json_encode($resultado);

?>

On my server it worked properly.

If this answer was helpful, I'll be happy to pick this answer or mark it as useful (+1).

I hope I have helped!

    
03.06.2016 / 18:45