Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

Hi I need a help on below, I know its raised in a past but I am currently struggling to figure it out the error Cannot use object of type stdClass as array on line

$score[$counter]=($bronze*$tempArray[6])+($silver*$tempArray[5])+($silver*$tempArray[4]);

<?php
       //turning the date other way around that is why explode the date string and stored in an Array
    $gold=$_GET['gold_input'];
    $silver=$_GET['silver_input'];
    $bronze=$_GET['bronze_input'];
    $gdp_value=$_GET['gdp_checked'];
    $link = new mysqli('localhost', 'root', '','coa123cdb');
    $myArray = array();
    //data format for information
//[{"name":"Ahmet Akdilek","country_name":"Turkey","gdp":"773091000000","population":"72752000"}
    $qury="SELECT  * FROM  coa123cdb.Country";

    $result=mysqli_query($link,$qury)
    or die("Error: ".mysqli_error($link));
    $row_cnt = $result->num_rows;
    if ($result = $link->query($qury)) {
    $tempArray = array();
    $scorex=array($row_cnt);
    $score=(object)$scorex;
    $counter=0  ;
    //while($row = $result->fetch_object()) {
    while($row=mysqli_fetch_object($result)){

      $tempArray = $row;
                if($gdp_value==0)
            {
            $score[$counter]=($bronze*$tempArray[6])+($silver*$tempArray[5])+($silver*$tempArray[4]);
            }
            else
            {$score[$counter]=($bronze*$tempArray[6]+$silver*$tempArray[5]+$silver*$tempArray[4])*$tempArray[1]/$tempArray[2]/10000;
            }
            array_push($tempArray, $score[$counter]);
            array_push($myArray, $tempArray);
                            $counter=$counter+1;
        }



        //var_dump($score);
    echo json_encode($myArray);
    }

    $result->close();
    $link->close();

  ?>
share|improve this question
    
Why didn't you include the line? –  Explosion Pills May 15 '13 at 1:31
    
I did below is the line I am getting an error on $score[$counter]=($bronze*$tempArray[6])+($silver*$tempArray[5])+($silver*$tempA‌​rray[4]); –  Shoaib Suleman May 15 '13 at 1:32

2 Answers 2

mysqli_fetch_object returns an object.

So after the line $row=mysqli_fetch_object($result) is an object.

If you want an array use mysqli_fetch_array instead.

And before using that array check its contents with var_dump($row); (to prevent further questions).

share|improve this answer

Take a look at how you declared $score.

First you do $scorex=array($row_cnt); and then $score=(object)$scorex;.

So $score is cast to an object. However, in your code, you are still addressing it like an array, i.e. $score[$counter]. You should reference it as an object.

EDIT

Alternatively, update your definition for $score to the following:

 $score = array_fill (0, $row_cnt, 0);

This way, your assignment to $score[$counter] will still work (i think in the way you intended).

share|improve this answer
    
I modify as advised $score[$counter]=($bronze*$tempArray->bronze)+($silver*$tempArray->silver)+($sil‌​ver*$tempArray->gold); but still getting the same error –  Shoaib Suleman May 15 '13 at 1:38
    
I can't use mysql_fetch_object, I have to use mysqli_fetch_object or $result->fetch_object() but on both getting the same error –  Shoaib Suleman May 15 '13 at 1:39
    
take a look at how you declared $score. First you do $scorex=array($row_cnt); and then $score=(object)$scorex;. So $score is cast to an object. However, in your code, you are still addressing it like an array, i.e. $score[$counter]. That also needs to change. –  Ryan May 15 '13 at 1:41
    
mysql_fetch_obj is deprecated, as are all the mysql_ functions - mysqli_fetch_obj is not. –  Sam Dufel May 15 '13 at 1:42
    
@ryan...thanks for your reply, but do you mind please telling us how can I change this to make it working –  Shoaib Suleman May 15 '13 at 1:45

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.