Cannot use object of type stdClass as array?





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







446















I get a strange error using json_decode(). It decode correctly the data (I saw it using print_r), but when I try to access to info inside the array I get:



Fatal error: Cannot use object of type stdClass as array in
C:UsersDailsoftwareabs.php on line 108


I only tried to do: $result['context'] where $result has the data returned by json_decode()



How can I read values inside this array?










share|improve this question




















  • 14





    $result = json_decode('the string', true); Adding the true returns the result as an array and not an stdClass.

    – Nepaluz
    Apr 19 '16 at 20:12




















446















I get a strange error using json_decode(). It decode correctly the data (I saw it using print_r), but when I try to access to info inside the array I get:



Fatal error: Cannot use object of type stdClass as array in
C:UsersDailsoftwareabs.php on line 108


I only tried to do: $result['context'] where $result has the data returned by json_decode()



How can I read values inside this array?










share|improve this question




















  • 14





    $result = json_decode('the string', true); Adding the true returns the result as an array and not an stdClass.

    – Nepaluz
    Apr 19 '16 at 20:12
















446












446








446


73






I get a strange error using json_decode(). It decode correctly the data (I saw it using print_r), but when I try to access to info inside the array I get:



Fatal error: Cannot use object of type stdClass as array in
C:UsersDailsoftwareabs.php on line 108


I only tried to do: $result['context'] where $result has the data returned by json_decode()



How can I read values inside this array?










share|improve this question
















I get a strange error using json_decode(). It decode correctly the data (I saw it using print_r), but when I try to access to info inside the array I get:



Fatal error: Cannot use object of type stdClass as array in
C:UsersDailsoftwareabs.php on line 108


I only tried to do: $result['context'] where $result has the data returned by json_decode()



How can I read values inside this array?







php json






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 16 '18 at 12:22









Donald Duck

4,072134262




4,072134262










asked Jul 25 '11 at 11:40









DailDail

2,2342103




2,2342103








  • 14





    $result = json_decode('the string', true); Adding the true returns the result as an array and not an stdClass.

    – Nepaluz
    Apr 19 '16 at 20:12
















  • 14





    $result = json_decode('the string', true); Adding the true returns the result as an array and not an stdClass.

    – Nepaluz
    Apr 19 '16 at 20:12










14




14





$result = json_decode('the string', true); Adding the true returns the result as an array and not an stdClass.

– Nepaluz
Apr 19 '16 at 20:12







$result = json_decode('the string', true); Adding the true returns the result as an array and not an stdClass.

– Nepaluz
Apr 19 '16 at 20:12














13 Answers
13






active

oldest

votes


















680














Use the second parameter of json_decode to make it return an array:



$result = json_decode($data, true);





share|improve this answer

































    186














    The function json_decode() returns an object by default.



    You can access the data like this:



    var_dump($result->context);


    If you have identifiers like from-date (the hyphen would cause a PHP error when using the above method) you have to write:



    var_dump($result->{'from-date'});


    If you want an array you can do something like this:



    $result = json_decode($json, true);


    Or cast the object to an array:



    $result = (array) json_decode($json);





    share|improve this answer



















    • 2





      took me a while to find this when trying to find a way to refer to the _destroy value in php that's set by knockoutjs, so +1

      – deltree
      Mar 28 '12 at 2:47








    • 1





      This answer is much more qualified than first (most rated) answer!

      – Mojtaba Rezaeian
      Dec 5 '18 at 7:28



















    126














    You must access it using -> since its an object.



    Change your code from:



    $result['context'];


    To:



    $result->context;





    share|improve this answer


























    • The problem I have is trying to use the property in a conditional if ($result->context = $var) This causes the property to be set to the var and returns true, no matter.

      – STWilson
      Nov 16 '16 at 21:28






    • 1





      @STWilson you should be using a double equals ==, in your current state you are assigning $var value to $result->context by using a single equal =. And the if statement will read it as if it is empty or not, and if the $var has value then that means it is not empty and will always return true.

      – JiNexus
      Nov 16 '16 at 23:26













    • Thanks! This should be marked as the correct answer...

      – Roberto
      Sep 2 '17 at 22:56



















    74














    Use true as the second parameter to json_decode. This will decode the json into an associative array instead of stdObject instances:



    $my_array = json_decode($my_json, true);


    See the documentation for more details.






    share|improve this answer































      70














      Have same problem today, solved like this:



      If you call json_decode($somestring) you will get an Object and you need to access like $object->key , but if u call json_decode($somestring, true) you will get an dictionary and can access like $array['key']






      share|improve this answer





















      • 1





        This saved me sooo much time! I was not putting in the true parameter, and trying to access it as an array

        – Meeyam
        Dec 7 '18 at 23:07



















      54














      It's not an array, it's an object of type stdClass.



      You can access it like this:



      echo $oResult->context;


      More info here: What is stdClass in PHP?






      share|improve this answer

































        22














        As the Php Manual say,




        print_r — Prints human-readable information about a variable




        When we use json_decode();, we get an object of type stdClass as return type.
        The arguments, which are to be passed inside of print_r() should either be an array or a string. Hence, we cannot pass an object inside of print_r(). I found 2 ways to deal with this.





        1. Cast the object to array.

          This can be achieved as follows.



          $a = (array)$object;



        2. By accessing the key of the Object

          As mentioned earlier, when you use json_decode(); function, it returns an Object of stdClass. you can access the elements of the object with the help of -> Operator.



          $value = $object->key;



        One, can also use multiple keys to extract the sub elements incase if the object has nested arrays.



        $value = $object->key1->key2->key3...;


        Their are other options to print_r() as well, like var_dump(); and var_export();



        P.S : Also, If you set the second parameter of the json_decode(); to true, it will automatically convert the object to an array();
        Here are some references:
        http://php.net/manual/en/function.print-r.php
        http://php.net/manual/en/function.var-dump.php
        http://php.net/manual/en/function.var-export.php






        share|improve this answer

































          7














          You can convert stdClass object to array like:



          $array = (array)$stdClass;


          stdClsss to array






          share|improve this answer

































            4














            Here is the function signature:



            mixed json_decode ( string $json [, bool $assoc = false [, int $depth = 512 [, int $options = 0 ]]] )


            When param is false, which is default, it will return an appropriate php type. You fetch the value of that type using object.method paradigm.



            When param is true, it will return associative arrays.



            It will return NULL on error.



            If you want to fetch value through array, set assoc to true.






            share|improve this answer































              4














              When you try to access it as $result['context'], you treating it as an array, the error it's telling you that you are actually dealing with an object, then you should access it as $result->context






              share|improve this answer

































                2














                instead of using the brackets use the object operator for example my array based on database object is created like this in a class called DB:



                class DB {
                private static $_instance = null;
                private $_pdo,
                $_query,
                $_error = false,
                $_results,
                $_count = 0;



                private function __construct() {
                try{
                $this->_pdo = new PDO('mysql:host=' . Config::get('mysql/host') .';dbname=' . Config::get('mysql/db') , Config::get('mysql/username') ,Config::get('mysql/password') );


                } catch(PDOException $e) {
                $this->_error = true;
                $newsMessage = 'Sorry. Database is off line';
                $pagetitle = 'Teknikal Tim - Database Error';
                $pagedescription = 'Teknikal Tim Database Error page';
                include_once 'dbdown.html.php';
                exit;
                }
                $headerinc = 'header.html.php';
                }

                public static function getInstance() {
                if(!isset(self::$_instance)) {
                self::$_instance = new DB();
                }

                return self::$_instance;

                }


                public function query($sql, $params = array()) {
                $this->_error = false;
                if($this->_query = $this->_pdo->prepare($sql)) {
                $x = 1;
                if(count($params)) {
                foreach($params as $param){
                $this->_query->bindValue($x, $param);
                $x++;
                }
                }
                }
                if($this->_query->execute()) {

                $this->_results = $this->_query->fetchAll(PDO::FETCH_OBJ);
                $this->_count = $this->_query->rowCount();

                }

                else{
                $this->_error = true;
                }

                return $this;
                }

                public function action($action, $table, $where = array()) {
                if(count($where) ===3) {
                $operators = array('=', '>', '<', '>=', '<=');

                $field = $where[0];
                $operator = $where[1];
                $value = $where[2];

                if(in_array($operator, $operators)) {
                $sql = "{$action} FROM {$table} WHERE {$field} = ?";

                if(!$this->query($sql, array($value))->error()) {
                return $this;
                }
                }

                }
                return false;
                }

                public function get($table, $where) {
                return $this->action('SELECT *', $table, $where);

                public function results() {
                return $this->_results;
                }

                public function first() {
                return $this->_results[0];
                }

                public function count() {
                return $this->_count;
                }

                }


                to access the information I use this code on the controller script:



                <?php
                $pagetitle = 'Teknikal Tim - Service Call Reservation';
                $pagedescription = 'Teknikal Tim Sevice Call Reservation Page';
                require_once $_SERVER['DOCUMENT_ROOT'] .'/core/init.php';
                $newsMessage = 'temp message';

                $servicecallsdb = DB::getInstance()->get('tt_service_calls', array('UserID',
                '=','$_SESSION['UserID']));

                if(!$servicecallsdb) {
                // $servicecalls = array('ID'=>'','ServiceCallDescription'=>'No Service Calls');
                } else {
                $servicecalls = $servicecallsdb->results();
                }
                include 'servicecalls.html.php';



                ?>


                then to display the information I check to see if servicecalls has been set and has a count greater than 0 remember it's not an array I am referencing so I access the records with the object operator "->" like this:



                <?php include $_SERVER['DOCUMENT_ROOT'] .'/includes/header.html.php';?>
                <!--Main content-->
                <div id="mainholder"> <!-- div so that page footer can have a minum height from the
                header -->
                <h1><?php if(isset($pagetitle)) htmlout($pagetitle);?></h1>
                <br>
                <br>
                <article>
                <h2></h2>
                </article>
                <?php
                if (isset($servicecalls)) {
                if (count ($servicecalls) > 0){
                foreach ($servicecalls as $servicecall) {
                echo '<a href="/servicecalls/?servicecall=' .$servicecall->ID .'">'
                .$servicecall->ServiceCallDescription .'</a>';
                }
                }else echo 'No service Calls';

                }

                ?>
                <a href="/servicecalls/?new=true">Raise New Service Call</a>
                </div> <!-- Main content end-->
                <?php include $_SERVER['DOCUMENT_ROOT'] .'/includes/footer.html.php'; ?>





                share|improve this answer

































                  2














                  I got this error out of the blue because my facebook login suddently stopped working (I had also changed hosts) and throwed this error. The fix is really easy



                  The issue was in this code



                    $response = (new FacebookRequest(
                  FacebookSession::newAppSession($this->appId, $this->appSecret),
                  'GET',
                  '/oauth/access_token',
                  $params
                  ))->execute()->getResponse(true);

                  if (isset($response['access_token'])) { <---- this line gave error
                  return new FacebookSession($response['access_token']);
                  }


                  Basically isset() function expect an array but instead it find an object. The simple solution is to convert PHP object to array using (array) quantifier. The following is the fixed code.



                    $response = (array) (new FacebookRequest(
                  FacebookSession::newAppSession($this->appId, $this->appSecret),
                  'GET',
                  '/oauth/access_token',
                  $params
                  ))->execute()->getResponse(true);


                  Note the use off array() quantifier in first line.






                  share|improve this answer

































                    0














                    Change it for



                    $results->fetch_array()





                    share|improve this answer






















                      protected by Community Oct 3 '18 at 15:05



                      Thank you for your interest in this question.
                      Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).



                      Would you like to answer one of these unanswered questions instead?














                      13 Answers
                      13






                      active

                      oldest

                      votes








                      13 Answers
                      13






                      active

                      oldest

                      votes









                      active

                      oldest

                      votes






                      active

                      oldest

                      votes









                      680














                      Use the second parameter of json_decode to make it return an array:



                      $result = json_decode($data, true);





                      share|improve this answer






























                        680














                        Use the second parameter of json_decode to make it return an array:



                        $result = json_decode($data, true);





                        share|improve this answer




























                          680












                          680








                          680







                          Use the second parameter of json_decode to make it return an array:



                          $result = json_decode($data, true);





                          share|improve this answer















                          Use the second parameter of json_decode to make it return an array:



                          $result = json_decode($data, true);






                          share|improve this answer














                          share|improve this answer



                          share|improve this answer








                          edited Feb 3 '14 at 21:08

























                          answered Jul 25 '11 at 11:43









                          JonJon

                          348k60615719




                          348k60615719

























                              186














                              The function json_decode() returns an object by default.



                              You can access the data like this:



                              var_dump($result->context);


                              If you have identifiers like from-date (the hyphen would cause a PHP error when using the above method) you have to write:



                              var_dump($result->{'from-date'});


                              If you want an array you can do something like this:



                              $result = json_decode($json, true);


                              Or cast the object to an array:



                              $result = (array) json_decode($json);





                              share|improve this answer



















                              • 2





                                took me a while to find this when trying to find a way to refer to the _destroy value in php that's set by knockoutjs, so +1

                                – deltree
                                Mar 28 '12 at 2:47








                              • 1





                                This answer is much more qualified than first (most rated) answer!

                                – Mojtaba Rezaeian
                                Dec 5 '18 at 7:28
















                              186














                              The function json_decode() returns an object by default.



                              You can access the data like this:



                              var_dump($result->context);


                              If you have identifiers like from-date (the hyphen would cause a PHP error when using the above method) you have to write:



                              var_dump($result->{'from-date'});


                              If you want an array you can do something like this:



                              $result = json_decode($json, true);


                              Or cast the object to an array:



                              $result = (array) json_decode($json);





                              share|improve this answer



















                              • 2





                                took me a while to find this when trying to find a way to refer to the _destroy value in php that's set by knockoutjs, so +1

                                – deltree
                                Mar 28 '12 at 2:47








                              • 1





                                This answer is much more qualified than first (most rated) answer!

                                – Mojtaba Rezaeian
                                Dec 5 '18 at 7:28














                              186












                              186








                              186







                              The function json_decode() returns an object by default.



                              You can access the data like this:



                              var_dump($result->context);


                              If you have identifiers like from-date (the hyphen would cause a PHP error when using the above method) you have to write:



                              var_dump($result->{'from-date'});


                              If you want an array you can do something like this:



                              $result = json_decode($json, true);


                              Or cast the object to an array:



                              $result = (array) json_decode($json);





                              share|improve this answer













                              The function json_decode() returns an object by default.



                              You can access the data like this:



                              var_dump($result->context);


                              If you have identifiers like from-date (the hyphen would cause a PHP error when using the above method) you have to write:



                              var_dump($result->{'from-date'});


                              If you want an array you can do something like this:



                              $result = json_decode($json, true);


                              Or cast the object to an array:



                              $result = (array) json_decode($json);






                              share|improve this answer












                              share|improve this answer



                              share|improve this answer










                              answered Jul 25 '11 at 11:44









                              svenssvens

                              9,14052953




                              9,14052953








                              • 2





                                took me a while to find this when trying to find a way to refer to the _destroy value in php that's set by knockoutjs, so +1

                                – deltree
                                Mar 28 '12 at 2:47








                              • 1





                                This answer is much more qualified than first (most rated) answer!

                                – Mojtaba Rezaeian
                                Dec 5 '18 at 7:28














                              • 2





                                took me a while to find this when trying to find a way to refer to the _destroy value in php that's set by knockoutjs, so +1

                                – deltree
                                Mar 28 '12 at 2:47








                              • 1





                                This answer is much more qualified than first (most rated) answer!

                                – Mojtaba Rezaeian
                                Dec 5 '18 at 7:28








                              2




                              2





                              took me a while to find this when trying to find a way to refer to the _destroy value in php that's set by knockoutjs, so +1

                              – deltree
                              Mar 28 '12 at 2:47







                              took me a while to find this when trying to find a way to refer to the _destroy value in php that's set by knockoutjs, so +1

                              – deltree
                              Mar 28 '12 at 2:47






                              1




                              1





                              This answer is much more qualified than first (most rated) answer!

                              – Mojtaba Rezaeian
                              Dec 5 '18 at 7:28





                              This answer is much more qualified than first (most rated) answer!

                              – Mojtaba Rezaeian
                              Dec 5 '18 at 7:28











                              126














                              You must access it using -> since its an object.



                              Change your code from:



                              $result['context'];


                              To:



                              $result->context;





                              share|improve this answer


























                              • The problem I have is trying to use the property in a conditional if ($result->context = $var) This causes the property to be set to the var and returns true, no matter.

                                – STWilson
                                Nov 16 '16 at 21:28






                              • 1





                                @STWilson you should be using a double equals ==, in your current state you are assigning $var value to $result->context by using a single equal =. And the if statement will read it as if it is empty or not, and if the $var has value then that means it is not empty and will always return true.

                                – JiNexus
                                Nov 16 '16 at 23:26













                              • Thanks! This should be marked as the correct answer...

                                – Roberto
                                Sep 2 '17 at 22:56
















                              126














                              You must access it using -> since its an object.



                              Change your code from:



                              $result['context'];


                              To:



                              $result->context;





                              share|improve this answer


























                              • The problem I have is trying to use the property in a conditional if ($result->context = $var) This causes the property to be set to the var and returns true, no matter.

                                – STWilson
                                Nov 16 '16 at 21:28






                              • 1





                                @STWilson you should be using a double equals ==, in your current state you are assigning $var value to $result->context by using a single equal =. And the if statement will read it as if it is empty or not, and if the $var has value then that means it is not empty and will always return true.

                                – JiNexus
                                Nov 16 '16 at 23:26













                              • Thanks! This should be marked as the correct answer...

                                – Roberto
                                Sep 2 '17 at 22:56














                              126












                              126








                              126







                              You must access it using -> since its an object.



                              Change your code from:



                              $result['context'];


                              To:



                              $result->context;





                              share|improve this answer















                              You must access it using -> since its an object.



                              Change your code from:



                              $result['context'];


                              To:



                              $result->context;






                              share|improve this answer














                              share|improve this answer



                              share|improve this answer








                              edited Jan 8 '16 at 6:09









                              SHAZ

                              2,35261827




                              2,35261827










                              answered May 15 '14 at 5:47









                              JiNexusJiNexus

                              1,91911416




                              1,91911416













                              • The problem I have is trying to use the property in a conditional if ($result->context = $var) This causes the property to be set to the var and returns true, no matter.

                                – STWilson
                                Nov 16 '16 at 21:28






                              • 1





                                @STWilson you should be using a double equals ==, in your current state you are assigning $var value to $result->context by using a single equal =. And the if statement will read it as if it is empty or not, and if the $var has value then that means it is not empty and will always return true.

                                – JiNexus
                                Nov 16 '16 at 23:26













                              • Thanks! This should be marked as the correct answer...

                                – Roberto
                                Sep 2 '17 at 22:56



















                              • The problem I have is trying to use the property in a conditional if ($result->context = $var) This causes the property to be set to the var and returns true, no matter.

                                – STWilson
                                Nov 16 '16 at 21:28






                              • 1





                                @STWilson you should be using a double equals ==, in your current state you are assigning $var value to $result->context by using a single equal =. And the if statement will read it as if it is empty or not, and if the $var has value then that means it is not empty and will always return true.

                                – JiNexus
                                Nov 16 '16 at 23:26













                              • Thanks! This should be marked as the correct answer...

                                – Roberto
                                Sep 2 '17 at 22:56

















                              The problem I have is trying to use the property in a conditional if ($result->context = $var) This causes the property to be set to the var and returns true, no matter.

                              – STWilson
                              Nov 16 '16 at 21:28





                              The problem I have is trying to use the property in a conditional if ($result->context = $var) This causes the property to be set to the var and returns true, no matter.

                              – STWilson
                              Nov 16 '16 at 21:28




                              1




                              1





                              @STWilson you should be using a double equals ==, in your current state you are assigning $var value to $result->context by using a single equal =. And the if statement will read it as if it is empty or not, and if the $var has value then that means it is not empty and will always return true.

                              – JiNexus
                              Nov 16 '16 at 23:26







                              @STWilson you should be using a double equals ==, in your current state you are assigning $var value to $result->context by using a single equal =. And the if statement will read it as if it is empty or not, and if the $var has value then that means it is not empty and will always return true.

                              – JiNexus
                              Nov 16 '16 at 23:26















                              Thanks! This should be marked as the correct answer...

                              – Roberto
                              Sep 2 '17 at 22:56





                              Thanks! This should be marked as the correct answer...

                              – Roberto
                              Sep 2 '17 at 22:56











                              74














                              Use true as the second parameter to json_decode. This will decode the json into an associative array instead of stdObject instances:



                              $my_array = json_decode($my_json, true);


                              See the documentation for more details.






                              share|improve this answer




























                                74














                                Use true as the second parameter to json_decode. This will decode the json into an associative array instead of stdObject instances:



                                $my_array = json_decode($my_json, true);


                                See the documentation for more details.






                                share|improve this answer


























                                  74












                                  74








                                  74







                                  Use true as the second parameter to json_decode. This will decode the json into an associative array instead of stdObject instances:



                                  $my_array = json_decode($my_json, true);


                                  See the documentation for more details.






                                  share|improve this answer













                                  Use true as the second parameter to json_decode. This will decode the json into an associative array instead of stdObject instances:



                                  $my_array = json_decode($my_json, true);


                                  See the documentation for more details.







                                  share|improve this answer












                                  share|improve this answer



                                  share|improve this answer










                                  answered Jul 25 '11 at 11:42









                                  Sander MarechalSander Marechal

                                  19.1k95483




                                  19.1k95483























                                      70














                                      Have same problem today, solved like this:



                                      If you call json_decode($somestring) you will get an Object and you need to access like $object->key , but if u call json_decode($somestring, true) you will get an dictionary and can access like $array['key']






                                      share|improve this answer





















                                      • 1





                                        This saved me sooo much time! I was not putting in the true parameter, and trying to access it as an array

                                        – Meeyam
                                        Dec 7 '18 at 23:07
















                                      70














                                      Have same problem today, solved like this:



                                      If you call json_decode($somestring) you will get an Object and you need to access like $object->key , but if u call json_decode($somestring, true) you will get an dictionary and can access like $array['key']






                                      share|improve this answer





















                                      • 1





                                        This saved me sooo much time! I was not putting in the true parameter, and trying to access it as an array

                                        – Meeyam
                                        Dec 7 '18 at 23:07














                                      70












                                      70








                                      70







                                      Have same problem today, solved like this:



                                      If you call json_decode($somestring) you will get an Object and you need to access like $object->key , but if u call json_decode($somestring, true) you will get an dictionary and can access like $array['key']






                                      share|improve this answer















                                      Have same problem today, solved like this:



                                      If you call json_decode($somestring) you will get an Object and you need to access like $object->key , but if u call json_decode($somestring, true) you will get an dictionary and can access like $array['key']







                                      share|improve this answer














                                      share|improve this answer



                                      share|improve this answer








                                      edited Oct 25 '17 at 9:49

























                                      answered Jan 11 '16 at 17:37









                                      Alexey LysenkoAlexey Lysenko

                                      945719




                                      945719








                                      • 1





                                        This saved me sooo much time! I was not putting in the true parameter, and trying to access it as an array

                                        – Meeyam
                                        Dec 7 '18 at 23:07














                                      • 1





                                        This saved me sooo much time! I was not putting in the true parameter, and trying to access it as an array

                                        – Meeyam
                                        Dec 7 '18 at 23:07








                                      1




                                      1





                                      This saved me sooo much time! I was not putting in the true parameter, and trying to access it as an array

                                      – Meeyam
                                      Dec 7 '18 at 23:07





                                      This saved me sooo much time! I was not putting in the true parameter, and trying to access it as an array

                                      – Meeyam
                                      Dec 7 '18 at 23:07











                                      54














                                      It's not an array, it's an object of type stdClass.



                                      You can access it like this:



                                      echo $oResult->context;


                                      More info here: What is stdClass in PHP?






                                      share|improve this answer






























                                        54














                                        It's not an array, it's an object of type stdClass.



                                        You can access it like this:



                                        echo $oResult->context;


                                        More info here: What is stdClass in PHP?






                                        share|improve this answer




























                                          54












                                          54








                                          54







                                          It's not an array, it's an object of type stdClass.



                                          You can access it like this:



                                          echo $oResult->context;


                                          More info here: What is stdClass in PHP?






                                          share|improve this answer















                                          It's not an array, it's an object of type stdClass.



                                          You can access it like this:



                                          echo $oResult->context;


                                          More info here: What is stdClass in PHP?







                                          share|improve this answer














                                          share|improve this answer



                                          share|improve this answer








                                          edited May 23 '17 at 12:26









                                          Community

                                          11




                                          11










                                          answered Jul 25 '11 at 11:42









                                          Wesley van OpdorpWesley van Opdorp

                                          13k43457




                                          13k43457























                                              22














                                              As the Php Manual say,




                                              print_r — Prints human-readable information about a variable




                                              When we use json_decode();, we get an object of type stdClass as return type.
                                              The arguments, which are to be passed inside of print_r() should either be an array or a string. Hence, we cannot pass an object inside of print_r(). I found 2 ways to deal with this.





                                              1. Cast the object to array.

                                                This can be achieved as follows.



                                                $a = (array)$object;



                                              2. By accessing the key of the Object

                                                As mentioned earlier, when you use json_decode(); function, it returns an Object of stdClass. you can access the elements of the object with the help of -> Operator.



                                                $value = $object->key;



                                              One, can also use multiple keys to extract the sub elements incase if the object has nested arrays.



                                              $value = $object->key1->key2->key3...;


                                              Their are other options to print_r() as well, like var_dump(); and var_export();



                                              P.S : Also, If you set the second parameter of the json_decode(); to true, it will automatically convert the object to an array();
                                              Here are some references:
                                              http://php.net/manual/en/function.print-r.php
                                              http://php.net/manual/en/function.var-dump.php
                                              http://php.net/manual/en/function.var-export.php






                                              share|improve this answer






























                                                22














                                                As the Php Manual say,




                                                print_r — Prints human-readable information about a variable




                                                When we use json_decode();, we get an object of type stdClass as return type.
                                                The arguments, which are to be passed inside of print_r() should either be an array or a string. Hence, we cannot pass an object inside of print_r(). I found 2 ways to deal with this.





                                                1. Cast the object to array.

                                                  This can be achieved as follows.



                                                  $a = (array)$object;



                                                2. By accessing the key of the Object

                                                  As mentioned earlier, when you use json_decode(); function, it returns an Object of stdClass. you can access the elements of the object with the help of -> Operator.



                                                  $value = $object->key;



                                                One, can also use multiple keys to extract the sub elements incase if the object has nested arrays.



                                                $value = $object->key1->key2->key3...;


                                                Their are other options to print_r() as well, like var_dump(); and var_export();



                                                P.S : Also, If you set the second parameter of the json_decode(); to true, it will automatically convert the object to an array();
                                                Here are some references:
                                                http://php.net/manual/en/function.print-r.php
                                                http://php.net/manual/en/function.var-dump.php
                                                http://php.net/manual/en/function.var-export.php






                                                share|improve this answer




























                                                  22












                                                  22








                                                  22







                                                  As the Php Manual say,




                                                  print_r — Prints human-readable information about a variable




                                                  When we use json_decode();, we get an object of type stdClass as return type.
                                                  The arguments, which are to be passed inside of print_r() should either be an array or a string. Hence, we cannot pass an object inside of print_r(). I found 2 ways to deal with this.





                                                  1. Cast the object to array.

                                                    This can be achieved as follows.



                                                    $a = (array)$object;



                                                  2. By accessing the key of the Object

                                                    As mentioned earlier, when you use json_decode(); function, it returns an Object of stdClass. you can access the elements of the object with the help of -> Operator.



                                                    $value = $object->key;



                                                  One, can also use multiple keys to extract the sub elements incase if the object has nested arrays.



                                                  $value = $object->key1->key2->key3...;


                                                  Their are other options to print_r() as well, like var_dump(); and var_export();



                                                  P.S : Also, If you set the second parameter of the json_decode(); to true, it will automatically convert the object to an array();
                                                  Here are some references:
                                                  http://php.net/manual/en/function.print-r.php
                                                  http://php.net/manual/en/function.var-dump.php
                                                  http://php.net/manual/en/function.var-export.php






                                                  share|improve this answer















                                                  As the Php Manual say,




                                                  print_r — Prints human-readable information about a variable




                                                  When we use json_decode();, we get an object of type stdClass as return type.
                                                  The arguments, which are to be passed inside of print_r() should either be an array or a string. Hence, we cannot pass an object inside of print_r(). I found 2 ways to deal with this.





                                                  1. Cast the object to array.

                                                    This can be achieved as follows.



                                                    $a = (array)$object;



                                                  2. By accessing the key of the Object

                                                    As mentioned earlier, when you use json_decode(); function, it returns an Object of stdClass. you can access the elements of the object with the help of -> Operator.



                                                    $value = $object->key;



                                                  One, can also use multiple keys to extract the sub elements incase if the object has nested arrays.



                                                  $value = $object->key1->key2->key3...;


                                                  Their are other options to print_r() as well, like var_dump(); and var_export();



                                                  P.S : Also, If you set the second parameter of the json_decode(); to true, it will automatically convert the object to an array();
                                                  Here are some references:
                                                  http://php.net/manual/en/function.print-r.php
                                                  http://php.net/manual/en/function.var-dump.php
                                                  http://php.net/manual/en/function.var-export.php







                                                  share|improve this answer














                                                  share|improve this answer



                                                  share|improve this answer








                                                  edited Mar 5 '16 at 13:41

























                                                  answered Mar 5 '16 at 13:27









                                                  M.S.PM.S.P

                                                  1,42221324




                                                  1,42221324























                                                      7














                                                      You can convert stdClass object to array like:



                                                      $array = (array)$stdClass;


                                                      stdClsss to array






                                                      share|improve this answer






























                                                        7














                                                        You can convert stdClass object to array like:



                                                        $array = (array)$stdClass;


                                                        stdClsss to array






                                                        share|improve this answer




























                                                          7












                                                          7








                                                          7







                                                          You can convert stdClass object to array like:



                                                          $array = (array)$stdClass;


                                                          stdClsss to array






                                                          share|improve this answer















                                                          You can convert stdClass object to array like:



                                                          $array = (array)$stdClass;


                                                          stdClsss to array







                                                          share|improve this answer














                                                          share|improve this answer



                                                          share|improve this answer








                                                          edited May 23 '17 at 12:10









                                                          Community

                                                          11




                                                          11










                                                          answered Sep 9 '15 at 8:55









                                                          Arnab HoreArnab Hore

                                                          2,14811432




                                                          2,14811432























                                                              4














                                                              Here is the function signature:



                                                              mixed json_decode ( string $json [, bool $assoc = false [, int $depth = 512 [, int $options = 0 ]]] )


                                                              When param is false, which is default, it will return an appropriate php type. You fetch the value of that type using object.method paradigm.



                                                              When param is true, it will return associative arrays.



                                                              It will return NULL on error.



                                                              If you want to fetch value through array, set assoc to true.






                                                              share|improve this answer




























                                                                4














                                                                Here is the function signature:



                                                                mixed json_decode ( string $json [, bool $assoc = false [, int $depth = 512 [, int $options = 0 ]]] )


                                                                When param is false, which is default, it will return an appropriate php type. You fetch the value of that type using object.method paradigm.



                                                                When param is true, it will return associative arrays.



                                                                It will return NULL on error.



                                                                If you want to fetch value through array, set assoc to true.






                                                                share|improve this answer


























                                                                  4












                                                                  4








                                                                  4







                                                                  Here is the function signature:



                                                                  mixed json_decode ( string $json [, bool $assoc = false [, int $depth = 512 [, int $options = 0 ]]] )


                                                                  When param is false, which is default, it will return an appropriate php type. You fetch the value of that type using object.method paradigm.



                                                                  When param is true, it will return associative arrays.



                                                                  It will return NULL on error.



                                                                  If you want to fetch value through array, set assoc to true.






                                                                  share|improve this answer













                                                                  Here is the function signature:



                                                                  mixed json_decode ( string $json [, bool $assoc = false [, int $depth = 512 [, int $options = 0 ]]] )


                                                                  When param is false, which is default, it will return an appropriate php type. You fetch the value of that type using object.method paradigm.



                                                                  When param is true, it will return associative arrays.



                                                                  It will return NULL on error.



                                                                  If you want to fetch value through array, set assoc to true.







                                                                  share|improve this answer












                                                                  share|improve this answer



                                                                  share|improve this answer










                                                                  answered Oct 28 '15 at 8:23









                                                                  robertrobert

                                                                  168119




                                                                  168119























                                                                      4














                                                                      When you try to access it as $result['context'], you treating it as an array, the error it's telling you that you are actually dealing with an object, then you should access it as $result->context






                                                                      share|improve this answer






























                                                                        4














                                                                        When you try to access it as $result['context'], you treating it as an array, the error it's telling you that you are actually dealing with an object, then you should access it as $result->context






                                                                        share|improve this answer




























                                                                          4












                                                                          4








                                                                          4







                                                                          When you try to access it as $result['context'], you treating it as an array, the error it's telling you that you are actually dealing with an object, then you should access it as $result->context






                                                                          share|improve this answer















                                                                          When you try to access it as $result['context'], you treating it as an array, the error it's telling you that you are actually dealing with an object, then you should access it as $result->context







                                                                          share|improve this answer














                                                                          share|improve this answer



                                                                          share|improve this answer








                                                                          edited Feb 8 '17 at 9:19









                                                                          Tom

                                                                          3,39562744




                                                                          3,39562744










                                                                          answered Feb 8 '17 at 9:11









                                                                          Midas MtileniMidas Mtileni

                                                                          414




                                                                          414























                                                                              2














                                                                              instead of using the brackets use the object operator for example my array based on database object is created like this in a class called DB:



                                                                              class DB {
                                                                              private static $_instance = null;
                                                                              private $_pdo,
                                                                              $_query,
                                                                              $_error = false,
                                                                              $_results,
                                                                              $_count = 0;



                                                                              private function __construct() {
                                                                              try{
                                                                              $this->_pdo = new PDO('mysql:host=' . Config::get('mysql/host') .';dbname=' . Config::get('mysql/db') , Config::get('mysql/username') ,Config::get('mysql/password') );


                                                                              } catch(PDOException $e) {
                                                                              $this->_error = true;
                                                                              $newsMessage = 'Sorry. Database is off line';
                                                                              $pagetitle = 'Teknikal Tim - Database Error';
                                                                              $pagedescription = 'Teknikal Tim Database Error page';
                                                                              include_once 'dbdown.html.php';
                                                                              exit;
                                                                              }
                                                                              $headerinc = 'header.html.php';
                                                                              }

                                                                              public static function getInstance() {
                                                                              if(!isset(self::$_instance)) {
                                                                              self::$_instance = new DB();
                                                                              }

                                                                              return self::$_instance;

                                                                              }


                                                                              public function query($sql, $params = array()) {
                                                                              $this->_error = false;
                                                                              if($this->_query = $this->_pdo->prepare($sql)) {
                                                                              $x = 1;
                                                                              if(count($params)) {
                                                                              foreach($params as $param){
                                                                              $this->_query->bindValue($x, $param);
                                                                              $x++;
                                                                              }
                                                                              }
                                                                              }
                                                                              if($this->_query->execute()) {

                                                                              $this->_results = $this->_query->fetchAll(PDO::FETCH_OBJ);
                                                                              $this->_count = $this->_query->rowCount();

                                                                              }

                                                                              else{
                                                                              $this->_error = true;
                                                                              }

                                                                              return $this;
                                                                              }

                                                                              public function action($action, $table, $where = array()) {
                                                                              if(count($where) ===3) {
                                                                              $operators = array('=', '>', '<', '>=', '<=');

                                                                              $field = $where[0];
                                                                              $operator = $where[1];
                                                                              $value = $where[2];

                                                                              if(in_array($operator, $operators)) {
                                                                              $sql = "{$action} FROM {$table} WHERE {$field} = ?";

                                                                              if(!$this->query($sql, array($value))->error()) {
                                                                              return $this;
                                                                              }
                                                                              }

                                                                              }
                                                                              return false;
                                                                              }

                                                                              public function get($table, $where) {
                                                                              return $this->action('SELECT *', $table, $where);

                                                                              public function results() {
                                                                              return $this->_results;
                                                                              }

                                                                              public function first() {
                                                                              return $this->_results[0];
                                                                              }

                                                                              public function count() {
                                                                              return $this->_count;
                                                                              }

                                                                              }


                                                                              to access the information I use this code on the controller script:



                                                                              <?php
                                                                              $pagetitle = 'Teknikal Tim - Service Call Reservation';
                                                                              $pagedescription = 'Teknikal Tim Sevice Call Reservation Page';
                                                                              require_once $_SERVER['DOCUMENT_ROOT'] .'/core/init.php';
                                                                              $newsMessage = 'temp message';

                                                                              $servicecallsdb = DB::getInstance()->get('tt_service_calls', array('UserID',
                                                                              '=','$_SESSION['UserID']));

                                                                              if(!$servicecallsdb) {
                                                                              // $servicecalls = array('ID'=>'','ServiceCallDescription'=>'No Service Calls');
                                                                              } else {
                                                                              $servicecalls = $servicecallsdb->results();
                                                                              }
                                                                              include 'servicecalls.html.php';



                                                                              ?>


                                                                              then to display the information I check to see if servicecalls has been set and has a count greater than 0 remember it's not an array I am referencing so I access the records with the object operator "->" like this:



                                                                              <?php include $_SERVER['DOCUMENT_ROOT'] .'/includes/header.html.php';?>
                                                                              <!--Main content-->
                                                                              <div id="mainholder"> <!-- div so that page footer can have a minum height from the
                                                                              header -->
                                                                              <h1><?php if(isset($pagetitle)) htmlout($pagetitle);?></h1>
                                                                              <br>
                                                                              <br>
                                                                              <article>
                                                                              <h2></h2>
                                                                              </article>
                                                                              <?php
                                                                              if (isset($servicecalls)) {
                                                                              if (count ($servicecalls) > 0){
                                                                              foreach ($servicecalls as $servicecall) {
                                                                              echo '<a href="/servicecalls/?servicecall=' .$servicecall->ID .'">'
                                                                              .$servicecall->ServiceCallDescription .'</a>';
                                                                              }
                                                                              }else echo 'No service Calls';

                                                                              }

                                                                              ?>
                                                                              <a href="/servicecalls/?new=true">Raise New Service Call</a>
                                                                              </div> <!-- Main content end-->
                                                                              <?php include $_SERVER['DOCUMENT_ROOT'] .'/includes/footer.html.php'; ?>





                                                                              share|improve this answer






























                                                                                2














                                                                                instead of using the brackets use the object operator for example my array based on database object is created like this in a class called DB:



                                                                                class DB {
                                                                                private static $_instance = null;
                                                                                private $_pdo,
                                                                                $_query,
                                                                                $_error = false,
                                                                                $_results,
                                                                                $_count = 0;



                                                                                private function __construct() {
                                                                                try{
                                                                                $this->_pdo = new PDO('mysql:host=' . Config::get('mysql/host') .';dbname=' . Config::get('mysql/db') , Config::get('mysql/username') ,Config::get('mysql/password') );


                                                                                } catch(PDOException $e) {
                                                                                $this->_error = true;
                                                                                $newsMessage = 'Sorry. Database is off line';
                                                                                $pagetitle = 'Teknikal Tim - Database Error';
                                                                                $pagedescription = 'Teknikal Tim Database Error page';
                                                                                include_once 'dbdown.html.php';
                                                                                exit;
                                                                                }
                                                                                $headerinc = 'header.html.php';
                                                                                }

                                                                                public static function getInstance() {
                                                                                if(!isset(self::$_instance)) {
                                                                                self::$_instance = new DB();
                                                                                }

                                                                                return self::$_instance;

                                                                                }


                                                                                public function query($sql, $params = array()) {
                                                                                $this->_error = false;
                                                                                if($this->_query = $this->_pdo->prepare($sql)) {
                                                                                $x = 1;
                                                                                if(count($params)) {
                                                                                foreach($params as $param){
                                                                                $this->_query->bindValue($x, $param);
                                                                                $x++;
                                                                                }
                                                                                }
                                                                                }
                                                                                if($this->_query->execute()) {

                                                                                $this->_results = $this->_query->fetchAll(PDO::FETCH_OBJ);
                                                                                $this->_count = $this->_query->rowCount();

                                                                                }

                                                                                else{
                                                                                $this->_error = true;
                                                                                }

                                                                                return $this;
                                                                                }

                                                                                public function action($action, $table, $where = array()) {
                                                                                if(count($where) ===3) {
                                                                                $operators = array('=', '>', '<', '>=', '<=');

                                                                                $field = $where[0];
                                                                                $operator = $where[1];
                                                                                $value = $where[2];

                                                                                if(in_array($operator, $operators)) {
                                                                                $sql = "{$action} FROM {$table} WHERE {$field} = ?";

                                                                                if(!$this->query($sql, array($value))->error()) {
                                                                                return $this;
                                                                                }
                                                                                }

                                                                                }
                                                                                return false;
                                                                                }

                                                                                public function get($table, $where) {
                                                                                return $this->action('SELECT *', $table, $where);

                                                                                public function results() {
                                                                                return $this->_results;
                                                                                }

                                                                                public function first() {
                                                                                return $this->_results[0];
                                                                                }

                                                                                public function count() {
                                                                                return $this->_count;
                                                                                }

                                                                                }


                                                                                to access the information I use this code on the controller script:



                                                                                <?php
                                                                                $pagetitle = 'Teknikal Tim - Service Call Reservation';
                                                                                $pagedescription = 'Teknikal Tim Sevice Call Reservation Page';
                                                                                require_once $_SERVER['DOCUMENT_ROOT'] .'/core/init.php';
                                                                                $newsMessage = 'temp message';

                                                                                $servicecallsdb = DB::getInstance()->get('tt_service_calls', array('UserID',
                                                                                '=','$_SESSION['UserID']));

                                                                                if(!$servicecallsdb) {
                                                                                // $servicecalls = array('ID'=>'','ServiceCallDescription'=>'No Service Calls');
                                                                                } else {
                                                                                $servicecalls = $servicecallsdb->results();
                                                                                }
                                                                                include 'servicecalls.html.php';



                                                                                ?>


                                                                                then to display the information I check to see if servicecalls has been set and has a count greater than 0 remember it's not an array I am referencing so I access the records with the object operator "->" like this:



                                                                                <?php include $_SERVER['DOCUMENT_ROOT'] .'/includes/header.html.php';?>
                                                                                <!--Main content-->
                                                                                <div id="mainholder"> <!-- div so that page footer can have a minum height from the
                                                                                header -->
                                                                                <h1><?php if(isset($pagetitle)) htmlout($pagetitle);?></h1>
                                                                                <br>
                                                                                <br>
                                                                                <article>
                                                                                <h2></h2>
                                                                                </article>
                                                                                <?php
                                                                                if (isset($servicecalls)) {
                                                                                if (count ($servicecalls) > 0){
                                                                                foreach ($servicecalls as $servicecall) {
                                                                                echo '<a href="/servicecalls/?servicecall=' .$servicecall->ID .'">'
                                                                                .$servicecall->ServiceCallDescription .'</a>';
                                                                                }
                                                                                }else echo 'No service Calls';

                                                                                }

                                                                                ?>
                                                                                <a href="/servicecalls/?new=true">Raise New Service Call</a>
                                                                                </div> <!-- Main content end-->
                                                                                <?php include $_SERVER['DOCUMENT_ROOT'] .'/includes/footer.html.php'; ?>





                                                                                share|improve this answer




























                                                                                  2












                                                                                  2








                                                                                  2







                                                                                  instead of using the brackets use the object operator for example my array based on database object is created like this in a class called DB:



                                                                                  class DB {
                                                                                  private static $_instance = null;
                                                                                  private $_pdo,
                                                                                  $_query,
                                                                                  $_error = false,
                                                                                  $_results,
                                                                                  $_count = 0;



                                                                                  private function __construct() {
                                                                                  try{
                                                                                  $this->_pdo = new PDO('mysql:host=' . Config::get('mysql/host') .';dbname=' . Config::get('mysql/db') , Config::get('mysql/username') ,Config::get('mysql/password') );


                                                                                  } catch(PDOException $e) {
                                                                                  $this->_error = true;
                                                                                  $newsMessage = 'Sorry. Database is off line';
                                                                                  $pagetitle = 'Teknikal Tim - Database Error';
                                                                                  $pagedescription = 'Teknikal Tim Database Error page';
                                                                                  include_once 'dbdown.html.php';
                                                                                  exit;
                                                                                  }
                                                                                  $headerinc = 'header.html.php';
                                                                                  }

                                                                                  public static function getInstance() {
                                                                                  if(!isset(self::$_instance)) {
                                                                                  self::$_instance = new DB();
                                                                                  }

                                                                                  return self::$_instance;

                                                                                  }


                                                                                  public function query($sql, $params = array()) {
                                                                                  $this->_error = false;
                                                                                  if($this->_query = $this->_pdo->prepare($sql)) {
                                                                                  $x = 1;
                                                                                  if(count($params)) {
                                                                                  foreach($params as $param){
                                                                                  $this->_query->bindValue($x, $param);
                                                                                  $x++;
                                                                                  }
                                                                                  }
                                                                                  }
                                                                                  if($this->_query->execute()) {

                                                                                  $this->_results = $this->_query->fetchAll(PDO::FETCH_OBJ);
                                                                                  $this->_count = $this->_query->rowCount();

                                                                                  }

                                                                                  else{
                                                                                  $this->_error = true;
                                                                                  }

                                                                                  return $this;
                                                                                  }

                                                                                  public function action($action, $table, $where = array()) {
                                                                                  if(count($where) ===3) {
                                                                                  $operators = array('=', '>', '<', '>=', '<=');

                                                                                  $field = $where[0];
                                                                                  $operator = $where[1];
                                                                                  $value = $where[2];

                                                                                  if(in_array($operator, $operators)) {
                                                                                  $sql = "{$action} FROM {$table} WHERE {$field} = ?";

                                                                                  if(!$this->query($sql, array($value))->error()) {
                                                                                  return $this;
                                                                                  }
                                                                                  }

                                                                                  }
                                                                                  return false;
                                                                                  }

                                                                                  public function get($table, $where) {
                                                                                  return $this->action('SELECT *', $table, $where);

                                                                                  public function results() {
                                                                                  return $this->_results;
                                                                                  }

                                                                                  public function first() {
                                                                                  return $this->_results[0];
                                                                                  }

                                                                                  public function count() {
                                                                                  return $this->_count;
                                                                                  }

                                                                                  }


                                                                                  to access the information I use this code on the controller script:



                                                                                  <?php
                                                                                  $pagetitle = 'Teknikal Tim - Service Call Reservation';
                                                                                  $pagedescription = 'Teknikal Tim Sevice Call Reservation Page';
                                                                                  require_once $_SERVER['DOCUMENT_ROOT'] .'/core/init.php';
                                                                                  $newsMessage = 'temp message';

                                                                                  $servicecallsdb = DB::getInstance()->get('tt_service_calls', array('UserID',
                                                                                  '=','$_SESSION['UserID']));

                                                                                  if(!$servicecallsdb) {
                                                                                  // $servicecalls = array('ID'=>'','ServiceCallDescription'=>'No Service Calls');
                                                                                  } else {
                                                                                  $servicecalls = $servicecallsdb->results();
                                                                                  }
                                                                                  include 'servicecalls.html.php';



                                                                                  ?>


                                                                                  then to display the information I check to see if servicecalls has been set and has a count greater than 0 remember it's not an array I am referencing so I access the records with the object operator "->" like this:



                                                                                  <?php include $_SERVER['DOCUMENT_ROOT'] .'/includes/header.html.php';?>
                                                                                  <!--Main content-->
                                                                                  <div id="mainholder"> <!-- div so that page footer can have a minum height from the
                                                                                  header -->
                                                                                  <h1><?php if(isset($pagetitle)) htmlout($pagetitle);?></h1>
                                                                                  <br>
                                                                                  <br>
                                                                                  <article>
                                                                                  <h2></h2>
                                                                                  </article>
                                                                                  <?php
                                                                                  if (isset($servicecalls)) {
                                                                                  if (count ($servicecalls) > 0){
                                                                                  foreach ($servicecalls as $servicecall) {
                                                                                  echo '<a href="/servicecalls/?servicecall=' .$servicecall->ID .'">'
                                                                                  .$servicecall->ServiceCallDescription .'</a>';
                                                                                  }
                                                                                  }else echo 'No service Calls';

                                                                                  }

                                                                                  ?>
                                                                                  <a href="/servicecalls/?new=true">Raise New Service Call</a>
                                                                                  </div> <!-- Main content end-->
                                                                                  <?php include $_SERVER['DOCUMENT_ROOT'] .'/includes/footer.html.php'; ?>





                                                                                  share|improve this answer















                                                                                  instead of using the brackets use the object operator for example my array based on database object is created like this in a class called DB:



                                                                                  class DB {
                                                                                  private static $_instance = null;
                                                                                  private $_pdo,
                                                                                  $_query,
                                                                                  $_error = false,
                                                                                  $_results,
                                                                                  $_count = 0;



                                                                                  private function __construct() {
                                                                                  try{
                                                                                  $this->_pdo = new PDO('mysql:host=' . Config::get('mysql/host') .';dbname=' . Config::get('mysql/db') , Config::get('mysql/username') ,Config::get('mysql/password') );


                                                                                  } catch(PDOException $e) {
                                                                                  $this->_error = true;
                                                                                  $newsMessage = 'Sorry. Database is off line';
                                                                                  $pagetitle = 'Teknikal Tim - Database Error';
                                                                                  $pagedescription = 'Teknikal Tim Database Error page';
                                                                                  include_once 'dbdown.html.php';
                                                                                  exit;
                                                                                  }
                                                                                  $headerinc = 'header.html.php';
                                                                                  }

                                                                                  public static function getInstance() {
                                                                                  if(!isset(self::$_instance)) {
                                                                                  self::$_instance = new DB();
                                                                                  }

                                                                                  return self::$_instance;

                                                                                  }


                                                                                  public function query($sql, $params = array()) {
                                                                                  $this->_error = false;
                                                                                  if($this->_query = $this->_pdo->prepare($sql)) {
                                                                                  $x = 1;
                                                                                  if(count($params)) {
                                                                                  foreach($params as $param){
                                                                                  $this->_query->bindValue($x, $param);
                                                                                  $x++;
                                                                                  }
                                                                                  }
                                                                                  }
                                                                                  if($this->_query->execute()) {

                                                                                  $this->_results = $this->_query->fetchAll(PDO::FETCH_OBJ);
                                                                                  $this->_count = $this->_query->rowCount();

                                                                                  }

                                                                                  else{
                                                                                  $this->_error = true;
                                                                                  }

                                                                                  return $this;
                                                                                  }

                                                                                  public function action($action, $table, $where = array()) {
                                                                                  if(count($where) ===3) {
                                                                                  $operators = array('=', '>', '<', '>=', '<=');

                                                                                  $field = $where[0];
                                                                                  $operator = $where[1];
                                                                                  $value = $where[2];

                                                                                  if(in_array($operator, $operators)) {
                                                                                  $sql = "{$action} FROM {$table} WHERE {$field} = ?";

                                                                                  if(!$this->query($sql, array($value))->error()) {
                                                                                  return $this;
                                                                                  }
                                                                                  }

                                                                                  }
                                                                                  return false;
                                                                                  }

                                                                                  public function get($table, $where) {
                                                                                  return $this->action('SELECT *', $table, $where);

                                                                                  public function results() {
                                                                                  return $this->_results;
                                                                                  }

                                                                                  public function first() {
                                                                                  return $this->_results[0];
                                                                                  }

                                                                                  public function count() {
                                                                                  return $this->_count;
                                                                                  }

                                                                                  }


                                                                                  to access the information I use this code on the controller script:



                                                                                  <?php
                                                                                  $pagetitle = 'Teknikal Tim - Service Call Reservation';
                                                                                  $pagedescription = 'Teknikal Tim Sevice Call Reservation Page';
                                                                                  require_once $_SERVER['DOCUMENT_ROOT'] .'/core/init.php';
                                                                                  $newsMessage = 'temp message';

                                                                                  $servicecallsdb = DB::getInstance()->get('tt_service_calls', array('UserID',
                                                                                  '=','$_SESSION['UserID']));

                                                                                  if(!$servicecallsdb) {
                                                                                  // $servicecalls = array('ID'=>'','ServiceCallDescription'=>'No Service Calls');
                                                                                  } else {
                                                                                  $servicecalls = $servicecallsdb->results();
                                                                                  }
                                                                                  include 'servicecalls.html.php';



                                                                                  ?>


                                                                                  then to display the information I check to see if servicecalls has been set and has a count greater than 0 remember it's not an array I am referencing so I access the records with the object operator "->" like this:



                                                                                  <?php include $_SERVER['DOCUMENT_ROOT'] .'/includes/header.html.php';?>
                                                                                  <!--Main content-->
                                                                                  <div id="mainholder"> <!-- div so that page footer can have a minum height from the
                                                                                  header -->
                                                                                  <h1><?php if(isset($pagetitle)) htmlout($pagetitle);?></h1>
                                                                                  <br>
                                                                                  <br>
                                                                                  <article>
                                                                                  <h2></h2>
                                                                                  </article>
                                                                                  <?php
                                                                                  if (isset($servicecalls)) {
                                                                                  if (count ($servicecalls) > 0){
                                                                                  foreach ($servicecalls as $servicecall) {
                                                                                  echo '<a href="/servicecalls/?servicecall=' .$servicecall->ID .'">'
                                                                                  .$servicecall->ServiceCallDescription .'</a>';
                                                                                  }
                                                                                  }else echo 'No service Calls';

                                                                                  }

                                                                                  ?>
                                                                                  <a href="/servicecalls/?new=true">Raise New Service Call</a>
                                                                                  </div> <!-- Main content end-->
                                                                                  <?php include $_SERVER['DOCUMENT_ROOT'] .'/includes/footer.html.php'; ?>






                                                                                  share|improve this answer














                                                                                  share|improve this answer



                                                                                  share|improve this answer








                                                                                  edited Mar 7 '14 at 22:09

























                                                                                  answered Mar 7 '14 at 22:00









                                                                                  timmac15timmac15

                                                                                  313




                                                                                  313























                                                                                      2














                                                                                      I got this error out of the blue because my facebook login suddently stopped working (I had also changed hosts) and throwed this error. The fix is really easy



                                                                                      The issue was in this code



                                                                                        $response = (new FacebookRequest(
                                                                                      FacebookSession::newAppSession($this->appId, $this->appSecret),
                                                                                      'GET',
                                                                                      '/oauth/access_token',
                                                                                      $params
                                                                                      ))->execute()->getResponse(true);

                                                                                      if (isset($response['access_token'])) { <---- this line gave error
                                                                                      return new FacebookSession($response['access_token']);
                                                                                      }


                                                                                      Basically isset() function expect an array but instead it find an object. The simple solution is to convert PHP object to array using (array) quantifier. The following is the fixed code.



                                                                                        $response = (array) (new FacebookRequest(
                                                                                      FacebookSession::newAppSession($this->appId, $this->appSecret),
                                                                                      'GET',
                                                                                      '/oauth/access_token',
                                                                                      $params
                                                                                      ))->execute()->getResponse(true);


                                                                                      Note the use off array() quantifier in first line.






                                                                                      share|improve this answer






























                                                                                        2














                                                                                        I got this error out of the blue because my facebook login suddently stopped working (I had also changed hosts) and throwed this error. The fix is really easy



                                                                                        The issue was in this code



                                                                                          $response = (new FacebookRequest(
                                                                                        FacebookSession::newAppSession($this->appId, $this->appSecret),
                                                                                        'GET',
                                                                                        '/oauth/access_token',
                                                                                        $params
                                                                                        ))->execute()->getResponse(true);

                                                                                        if (isset($response['access_token'])) { <---- this line gave error
                                                                                        return new FacebookSession($response['access_token']);
                                                                                        }


                                                                                        Basically isset() function expect an array but instead it find an object. The simple solution is to convert PHP object to array using (array) quantifier. The following is the fixed code.



                                                                                          $response = (array) (new FacebookRequest(
                                                                                        FacebookSession::newAppSession($this->appId, $this->appSecret),
                                                                                        'GET',
                                                                                        '/oauth/access_token',
                                                                                        $params
                                                                                        ))->execute()->getResponse(true);


                                                                                        Note the use off array() quantifier in first line.






                                                                                        share|improve this answer




























                                                                                          2












                                                                                          2








                                                                                          2







                                                                                          I got this error out of the blue because my facebook login suddently stopped working (I had also changed hosts) and throwed this error. The fix is really easy



                                                                                          The issue was in this code



                                                                                            $response = (new FacebookRequest(
                                                                                          FacebookSession::newAppSession($this->appId, $this->appSecret),
                                                                                          'GET',
                                                                                          '/oauth/access_token',
                                                                                          $params
                                                                                          ))->execute()->getResponse(true);

                                                                                          if (isset($response['access_token'])) { <---- this line gave error
                                                                                          return new FacebookSession($response['access_token']);
                                                                                          }


                                                                                          Basically isset() function expect an array but instead it find an object. The simple solution is to convert PHP object to array using (array) quantifier. The following is the fixed code.



                                                                                            $response = (array) (new FacebookRequest(
                                                                                          FacebookSession::newAppSession($this->appId, $this->appSecret),
                                                                                          'GET',
                                                                                          '/oauth/access_token',
                                                                                          $params
                                                                                          ))->execute()->getResponse(true);


                                                                                          Note the use off array() quantifier in first line.






                                                                                          share|improve this answer















                                                                                          I got this error out of the blue because my facebook login suddently stopped working (I had also changed hosts) and throwed this error. The fix is really easy



                                                                                          The issue was in this code



                                                                                            $response = (new FacebookRequest(
                                                                                          FacebookSession::newAppSession($this->appId, $this->appSecret),
                                                                                          'GET',
                                                                                          '/oauth/access_token',
                                                                                          $params
                                                                                          ))->execute()->getResponse(true);

                                                                                          if (isset($response['access_token'])) { <---- this line gave error
                                                                                          return new FacebookSession($response['access_token']);
                                                                                          }


                                                                                          Basically isset() function expect an array but instead it find an object. The simple solution is to convert PHP object to array using (array) quantifier. The following is the fixed code.



                                                                                            $response = (array) (new FacebookRequest(
                                                                                          FacebookSession::newAppSession($this->appId, $this->appSecret),
                                                                                          'GET',
                                                                                          '/oauth/access_token',
                                                                                          $params
                                                                                          ))->execute()->getResponse(true);


                                                                                          Note the use off array() quantifier in first line.







                                                                                          share|improve this answer














                                                                                          share|improve this answer



                                                                                          share|improve this answer








                                                                                          edited Aug 10 '17 at 4:46

























                                                                                          answered Jul 20 '17 at 15:44









                                                                                          Hammad KhanHammad Khan

                                                                                          9,3411287112




                                                                                          9,3411287112























                                                                                              0














                                                                                              Change it for



                                                                                              $results->fetch_array()





                                                                                              share|improve this answer




























                                                                                                0














                                                                                                Change it for



                                                                                                $results->fetch_array()





                                                                                                share|improve this answer


























                                                                                                  0












                                                                                                  0








                                                                                                  0







                                                                                                  Change it for



                                                                                                  $results->fetch_array()





                                                                                                  share|improve this answer













                                                                                                  Change it for



                                                                                                  $results->fetch_array()






                                                                                                  share|improve this answer












                                                                                                  share|improve this answer



                                                                                                  share|improve this answer










                                                                                                  answered Jul 5 '17 at 1:08









                                                                                                  Dan PadillaDan Padilla

                                                                                                  132




                                                                                                  132

















                                                                                                      protected by Community Oct 3 '18 at 15:05



                                                                                                      Thank you for your interest in this question.
                                                                                                      Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).



                                                                                                      Would you like to answer one of these unanswered questions instead?



                                                                                                      Popular posts from this blog

                                                                                                      Florida Star v. B. J. F.

                                                                                                      Danny Elfman

                                                                                                      Retrieve a Users Dashboard in Tumblr with R and TumblR. Oauth Issues