0

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]);

Code:

<?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"}
    $query = "SELECT  * FROM  coa123cdb.Country";

    $result = mysqli_query($link, $query)
    or die("Error: ".mysqli_error($link));
    $row_cnt = $result->num_rows;
    if ($result = $link->query($query)) {
    $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();

  ?>
2
  • Why didn't you include the line? Commented May 15, 2013 at 1:31
  • I did below is the line I am getting an error on $score[$counter]=($bronze*$tempArray[6])+($silver*$tempArray[5])+($silver*$tempArray[4]); Commented May 15, 2013 at 1:32

2 Answers 2

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).

Sign up to request clarification or add additional context in comments.

Comments

1

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).

7 Comments

I modify as advised $score[$counter]=($bronze*$tempArray->bronze)+($silver*$tempArray->silver)+($silver*$tempArray->gold); but still getting the same error
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
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.
mysql_fetch_obj is deprecated, as are all the mysql_ functions - mysqli_fetch_obj is not.
@ryan...thanks for your reply, but do you mind please telling us how can I change this to make it working
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.