www.webdeveloper.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 16

Thread: [RESOLVED] Invalid JSON: Fatal error: Cannot use object of type carrito as array

  1. #1
    Join Date
    Aug 2006
    Posts
    52

    resolved [RESOLVED] Invalid JSON: Fatal error: Cannot use object of type carrito as array

    Dear Friends,

    I've already checked a long list of details and can't find the solution.

    I have a website hosted on two different servers, with the same exactly OS and PHP version, while on one host the script works without any issue, on the other host I get the following error:


    "Invalid JSON: <br />
    <b>Fatal error</b>: Cannot use object of type carrito as array in <b>/home/username/public_html/lib/class/cart.class.php</b> on line <b>186</b><br />"

    What could it be?

    Any help will be much appreciated...
    D. Duarte

  2. #2
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,535
    We'll need to see line 186 and some preceding lines to have any likelihood of figuring out the root cause. All we know right now is that you are probably passing a variable to json_encode() which is an object of type "carrito", while json_encode() expects that variable to be an array.
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  3. #3
    Join Date
    Aug 2006
    Posts
    52
    Hello NogDog,

    I've searched for json_encode() strings before, following this http://blog.ryanrampersad.com/2010/0...lass-as-array/, and could'n find any.

    Here you have some code Before line 186:
    PHP Code:
                $sql mysql_query("
                                SELECT
                                    articulos.articuloid,
                                    articulos.articulo_titulo,
                                    articulos.articulo_descuento,
                                    articulos.articulo_precio,
                                    articulos.articulo_precio_oferta,
                                    articulos.articulo_imagen,
                                    articulos.articulo_url,
                                    articulos.articulo_estado,
                                    categorias.categoria_url
                                FROM
                                    articulos, categorias
                                WHERE
                                    articulos.articuloid='
    $id' and
                                    articulos.categoriaid=categorias.categoriaid"
                                
    ,$this->con);
                if (
    mysql_num_rows($sql))
                {
                    
    $art mysql_fetch_assoc($sql);
                    if (
    $art['articulo_estado'] == 1)
                    {

                        
    // obtener ultimo indice del array carrito
                        
    $i 0;
                        if (
    count($_SESSION['carrito']))
                        {
                            foreach(
    $_SESSION['carrito'] as $k => $v)
                            {
                                if (
    is_numeric($k))
                                {
                                    
    $i $k 1;
                                }
                            }

                            if (
    $i == 0$i 1;

                        }else
                        {
                            
    $i count($_SESSION['carrito']);
                        }

                        
    // imagen para mostrar en el carrito
                        
    if ($colores_sql_ok['color_imagen'])
                        {
                            
    $imagen 'colores/'.$colores_sql_ok['color_imagen'];
                        }else
                        {
                            
    $imagen $art['articulo_imagen'];
                        }

                        if (
    $art['articulo_precio_oferta'] > and $art['articulo_precio_oferta'] != $art['articulo_precio'])
                        {
                            
    $precio $art['articulo_precio_oferta'];
                        }else
                        {
                            
    $precio $art['articulo_precio'];
                        }


                        
    // finalmente, despues de todo, añadimos el articulo

    //line 186:
                        
    $_SESSION['carrito'][$i]['id']       = $art['articuloid'];

    //After line 186:

                        
    $_SESSION['carrito'][$i]['titulo']   = $art['articulo_titulo'];
                        
    $_SESSION['carrito'][$i]['dto']      = $art['articulo_descuento'];
                        
    $_SESSION['carrito'][$i]['precio']   = $precio;
                        
    $_SESSION['carrito'][$i]['imagen']   = $imagen;
                        
    $_SESSION['carrito'][$i]['url']      = $art['categoria_url'].'/'.$art['articulo_url'];
                        
    $_SESSION['carrito'][$i]['cantidad'] = $cantidad;

                        
    $_SESSION['carrito'][$i]['talla']['id']     = $talla['id'];
                        
    $_SESSION['carrito'][$i]['talla']['titulo'] = $talla['titulo'];
                        
    $_SESSION['carrito'][$i]['color']['id']     = $color['id'];
                        
    $_SESSION['carrito'][$i]['color']['titulo'] = $color['titulo'];

                        if (
    $_SESSION['user']['dto'] > $art['articulo_descuento'])
                        {
                            
    $art['articulo_descuento'] = $_SESSION['user']['dto'];
                            
    $_SESSION['carrito'][$i]['dto'] = $art['articulo_descuento'];
                        }

                        
    // total?
                        
    $precio $this->aplicar_dto($precio,$art['articulo_descuento']);
                        
    $total $precio[1] * $cantidad;

                        
    $_SESSION['carrito'][$i]['total'] = $total;
                        
    $_SESSION['carrito'][$i]['ahorro'] = $precio[2];

                        
    $result true;

                        
    $_SESSION['compra']['exito'] = false;


                    }else
                    {
                        
    // el articulo no está activo
                        
    $this->errors[] = 'Lo sentimos, este artículo no está disponible';
                    }


                }else
                {
                    
    $this->errors[] = 'Ups! parece que no hemos encontrado el artículo que buscas...';
                }
                if (
    $sqlmysql_free_result($sql);
            }


            return 
    $result;
        } 

    You have the entire file attached.

    Thanks,
    D. Duarte
    Attached Files Attached Files
    Last edited by NogDog; 12-12-2013 at 05:07 PM. Reason: added [php] tags

  4. #4
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,535
    My best guess at this point is some other file earlier in the process is setting $_SESSION['carrito'] incorrectly, but you aren't seeing the problem until the point where it tries to use it within that class.
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  5. #5
    Join Date
    Jul 2012
    Location
    Vancouver
    Posts
    61
    That's a type error.. you usually see that when trying to use StdClass to an array...

    On line 186 you're trying to declare $_SESSION['carrito'][$i]['id'] = $art['articuloid'];

    I'm assuming that $_SESSION['carrito'][$i] is a object and not an array... so you'd have to do $_SESSION['carrito'][$i]->id = $art['articuloid'];

    OR $_SESSION['carrito'] is the object and you'd have to do $_SESSION['carrito']->$i->id. Or just cast the whole variable

    $_SESSION['carrito'] = (array) $_SESSION['carrito']; // put this at line 185
    Last edited by mistin.ca; 12-13-2013 at 07:31 AM.

  6. #6
    Join Date
    Aug 2006
    Posts
    52
    Hello to all,

    I'm not able to track the error

    What I don't understand is why this script works well on one host and not on another, they have the same Apache and PHP versions...

    D. Duarte

  7. #7
    Join Date
    Aug 2006
    Posts
    52
    $_SESSION['carrito'] = array(); is set on the index.php file, can this be the cause?

  8. #8
    Join Date
    Mar 2007
    Location
    localhost
    Posts
    2,407
    count() counts countable items, example $ok = count(false); would return 1 to $ok, so my thoughts are that the $_SESSION['carrito'] is not an array or object but some other variable.

    Perhaps it is a serialized string still and has not been properly extracted.
    Yes, I know I'm about as subtle as being hit by a bus..(\\.\ Aug08)
    Yep... I say it like I see it, even if it is like a baseball bat in the nutz... (\\.\ Aug08)
    I want to leave this world the same way I came into it, Screaming, Incontinent & No memory!
    I laughed that hard I burst my colostomy bag... (\\.\ May03)
    Life for some is like a car accident... Mine is like a motorway pile up...

    Problems with Vista? :: Getting Cryptic wid it. :: The 'C' word! :: Whois?

  9. #9
    Join Date
    Aug 2006
    Posts
    52
    Any suggestion for why this script works well on one host and not on another, with the same Apache and PHP versions...
    Maybe understanding this could help to fix the issue...

  10. #10
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,535
    Quote Originally Posted by drtduarte View Post
    Any suggestion for why this script works well on one host and not on another, with the same Apache and PHP versions...
    Maybe understanding this could help to fix the issue...
    Some possibilities:

    1. Something changed in the code for one of them, maybe a different version of the software you're using, or an inadvertent change. (Maybe reload the application files?)

    2. You're using a different version of PHP, and that has some obscure side effect (maybe a bug fix) that's causing a difference.

    3. Different OS (some things work differently under Windows Server than they do on Linux -- not many, but there are some)
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  11. #11
    Join Date
    Dec 2011
    Location
    Centurion, South Africa
    Posts
    795
    Make sure the same PHP modules have been included on each server, JSON for example.

    You can check modules loaded and typically their versions by using:

    info.php
    PHP Code:
    <?php

        phpinfo
    ();

    ?>
    Be sure to delete the file once you've determined everything is the same.

  12. #12
    Join Date
    Aug 2006
    Posts
    52
    Some more funny info.

    Working perfectly on Firefox and IE, just doesn't work on Chrome...

  13. #13
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,535
    Anything different in the browsers' security/privacy settings regarding cookies?

    Are you using the same exact domain name in each instance, including the presence or lack of a "www." sub-domain?

    Either of those might affect the session cookie exchange. For the latter, it's a good idea to set session.cookie_domain to ".yourdomain.com" (note the leading dot) so the cookie gets exchanged regardless of whether or not "www." is used.
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  14. #14
    Join Date
    Aug 2006
    Posts
    52
    FIXED!!!

    Thank you all for your help!

    So the server was using the same PHP version but not the same php config, so I used a php.ini file to put the same config. The problem is that bad cookies were already saved by the browser with the old php settings...

    That's why the script was working on Firefox and IE, as I only used these browsers after changing php.ini settings...

    After deleting cookies from chrome, the error is gone. I was not able to understand exactly what setting is causing this issue, but I think I will never know...

    Thank you all
    D. Duarte

  15. #15
    Join Date
    Aug 2006
    Posts
    52
    After all my curiosity was killing me, so I decided to compare each setting step by step and fighured out that register_globals ON was the setting causing the issue, as almost all the time, the easy issues are the harder to find...

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles