0

I am starting to learn php for interaction with mysql db.

I have to fetch data from two unrelated tables and form an array of stdClass objects then dump it as json array.

I have so far managed to fetch data from table1 and added some columns from it into an myobjects[], as follows..

Here $array is an array of primary keys , also i pass a reference to myobjects array

  function  load_from_table1($array , &$myobjects)
  {      
        foreach($array as $num)
        {

            $obj=(object)null;
            $obj->prop1 = $num;

            $sql="SELECT * FROM `table1` WHERE col1=".$num;
            $result = mysqli_query($con, $sql);

            if($result!=null)
            {
                $row = mysqli_fetch_assoc($result);

                //inserting data into object
                $obj->prop2 = $row['col2'];
                $obj->prop3 = $row['col3'];
                $obj->prop4 = $row['col4'];

            }   


        $myobjects[]=$obj;
        } 
    }

It is fine so far now i need add two more properties to all items in myobjects array obtained from table2.

function load_from_table2(&$data)
{
    for($i=0;$i<sizeof($data);$i++)
    {

        $obj=(object)$data[$i];
        $id=$obj->prop1;

        $sql="SELECT * FROM `table2` WHERE col1=".$id;

        $result = mysqli_query($con, $sql);

        if($result!=null)
        {
           $row = mysqli_fetch_assoc($result);

           //$temp=$row['name'];
            //echo $temp;
            //$obj->name = "test1";

            $obj->name = $row['name'];

            //$temp=$row['description'];
            //echo $temp;
            //$obj->description = "test2";

            $obj->description = $row['description'];

        } 
   }

When i dump myobjects as json there is no output. But $temp echos properly , also when i us direct values every thing seems to work fine.

Can some one help me with this, also an explanation on what i am doing wrong would be greatly appreciated. Thank You.

Some Details on the working Environment , Because of the answer i provided ,
I am using wampserver 2.5 64 bit , and for now executing the php file off firefox browser.

The situation is quiet confusing as i can read the value and print it or save to variable , but not save it as object property.

Deepak
  • 1,238
  • 3
  • 22
  • 47
  • In your 2nd function, when you do `$obj=(object)$data[$i];`, you are not making `$obj` into a reference. So when you do `$obj->name = $row['name'];`, you are *not* adding into `$data`, you are just adding it to `$obj` (a copy of the array value that was converted into an object). – gen_Eric Dec 30 '14 at 15:47
  • 1
    @Rocket Haxmat i understand ur logic, but in that case the following should also not work , but they do; $obj->test = "test"; or $temp = "test" ; $obj->test = $temp; – Deepak Dec 31 '14 at 05:18

1 Answers1

0

I got it to work, But the problem was not with the code. It seems that the column in the table that i was reading has collation set to ' utf8_general_ci ' not sure what that means. But when i removed that setting , it all worked as expected. But i feel the collation is not the actual underlying reason. So i will keep this question open in hopes some might provide an explanation.

It seems i was looking for the wrong issues. Reading up on collation i found that some character sets are not supported fully , so i tried to echo the contents of the object array instead of json_encode and found it printed.

searching to that end i found another qst here on stackoverflow that solved my problem. Simply added this line :: mysqli_set_charset($con,'utf8'); before i started any queries on that table and json_encode started working.

Community
  • 1
  • 1
Deepak
  • 1,238
  • 3
  • 22
  • 47
  • Please check my question above for details on my database. – Deepak Dec 31 '14 at 19:54
  • i am choosing this as answer , so that any one unlucky enough to face same kind of issue can find it and save them a lot of time. – Deepak Jan 01 '15 at 15:06