-2

Hello and good evening,

I writing a simple PHP ecommerce website and I've stumbled upon a problem while doing the administration portion of it. I'm currently trying to do an AJAX request to fill some fields on a bootstrap modal and for that I go get the data from a database:

function editProductResponse($productId)
{
    require 'app/database/Database.php';
    require 'app/models/Product.php';
    require 'app/models/ProductImage.php';

    $product = new Product();
    $object = $product->getProduct($productId);
    $data = $object[0];
    $array = [
        "id" => $data->id,
        "name" => $data->name,
        //"description" => $data->description,
        "price" => $data->price,
        "stock" => $data->stock,
        "sold" => $data->sold,
        "added" => $data->added,
        "type" => $data->type,
        "subtype" => $data->subtype,
        "discount" => $data->discount
    ];
    echo print_r($data);
    echo json_encode($array);
}

That's my ajax.php function that gets that actual data and this works fine (bear in mind the commented part of the array is not a mistake, I'll explain that bellow; also i'm doing an array for debug purposes):

stdClass Object
(
    [id] => 1
    [name] => MSI Nightblade MI2-027XES Gaming
    [description] => Forjado com a paix�o pelo jogo, o MSI Nightblade MI2 foi feito para aqueles que anseiam por uma experi�ncia de jogo imersiva. Com armazenamento em abund�ncia, refrigera��o eficiente e gr�ficos de alta qualidade, esta pequena m�quina gaming est� pronta para desbloquear a sua pr�xima aventura gaming.
    [price] => 799
    [stock] => 147
    [sold] => 32
    [added] => 2010-11-22
    [type] => COMPUTER
    [subtype] => DESKTOP
    [discount] => 10
)
1{"id":1,"name":"MSI Nightblade MI2-027XES Gaming","price":799,"stock":147,"sold":32,"added":"2010-11-22","type":"COMPUTER","subtype":"DESKTOP","0":"discount => 10"}

This is my response in the browser log. Everything appears to be working fine and it is. So my problem is when I actually uncomment the description part from the array. If I do so, I get this:

stdClass Object
(
    [id] => 1
    [name] => MSI Nightblade MI2-027XES Gaming
    [description] => Forjado com a paix�o pelo jogo, o MSI Nightblade MI2 foi feito para aqueles que anseiam por uma experi�ncia de jogo imersiva. Com armazenamento em abund�ncia, refrigera��o eficiente e gr�ficos de alta qualidade, esta pequena m�quina gaming est� pronta para desbloquear a sua pr�xima aventura gaming.
    [price] => 799
    [stock] => 147
    [sold] => 32
    [added] => 2010-11-22
    [type] => COMPUTER
    [subtype] => DESKTOP
    [discount] => 10
)
1

I left the echo print_r to avoid any confusion on both results.

After I got this response it made me wonder if there's any sort of limit to JSON or if there's some sort of problem with the TEXT SQL data type as the description field is of this type.

As requested, this is the debug response of the actual array:

Array
(
    [id] => 1
    [name] => MSI Nightblade MI2-027XES Gaming
    [description] => Forjado com a paix�o pelo jogo, o MSI Nightblade MI2 foi feito para aqueles que anseiam por uma experi�ncia de jogo imersiva. Com armazenamento em abund�ncia, refrigera��o eficiente e gr�ficos de alta qualidade, esta pequena m�quina gaming est� pronta para desbloquear a sua pr�xima aventura gaming.
    [price] => 799
    [stock] => 147
    [sold] => 32
    [added] => 2010-11-22
    [type] => COMPUTER
    [subtype] => DESKTOP
    [discount] => 10
)
1
EpaXapate
  • 171
  • 10
  • so... what's the problem? the invalid characters? No, there's no... limit to how big a json string can be. at least, no limit that's any different to the max length a string can be. – Kevin B Dec 05 '16 at 22:04
  • the problem is exactly that, if I add the description there's an empty response.. what I can't figure out is why – EpaXapate Dec 05 '16 at 22:11
  • print the array – Kevin B Dec 05 '16 at 22:24
  • yeah I saw that just now, already corrected it, I'm editing the question. thank you @KevinB – EpaXapate Dec 05 '16 at 22:30
  • @KevinB as you requested I included the response when I print the array – EpaXapate Dec 05 '16 at 22:35
  • I don't see anything that would be causing what you are seeing, but it certainly isn't due to the length. It may be due to the value of the description, but i kinda doubt that too (and you can easily rule that out) – Kevin B Dec 05 '16 at 22:38
  • hmm... encoding problem maybe? http://stackoverflow.com/questions/20694317/json-encode-function-special-characters – Kevin B Dec 05 '16 at 22:39
  • That's what I was just thinking. To be sure, though, see what the error is with your JSON when you try to encode it: `var_dump(json_last_error());` This should output `5`. – robere2 Dec 05 '16 at 22:41

1 Answers1

0

I figured out what was causing the issue. There was a problem with some of my encoding in the database as I had some portuguese special characters that weren't correctly converted.

EpaXapate
  • 171
  • 10