0

I am trying to query database and keep count of records that have the same data in the campaign field. I am using an array to store counts. I first check to see if data from database is in the array. If it is not, add to the array as key and value of 1, if the data is already an key, increment value by 1. I am getting an error Fatal error: Cannot use assign-op operators with overloaded objects nor string offsets in on line 18

    <?php
include 'database/dbinfo.php';
$campaignCalls = array();

        //Connect to database
      $con=mysqli_connect("localhost",$username,$password,$database);
        // Check connection
        if (mysqli_connect_errno())
          {
          echo "Failed to connect to MySQL: " . mysqli_connect_error();
          }
        //Query records from database
        $result = mysqli_query($con, "SELECT * FROM  `phone_records` ");
        while($row = mysqli_fetch_array($result))
          {
          $campaign = $row['campaign'];
          //Check to see if data of field campaing is in array, if so increment by 1
          if (array_key_exists($campaign, $campaignCalls)){
              $campaign[$campaignCalls] += 1;
          }
          //Add key and value to array
          else {
              $campaignCalls[$campaign] = 1;
          }
          }
        mysqli_close($con);
        ?>
2
  • You may also want to include a tag which shows exactly which (programming) language this code is written in. It looks like the SQL version you are working with is MySQL (correct me if I am wrong) but I have no idea what language the rest of your code is in. Commented Jul 21, 2014 at 16:00
  • Adam,you are correct. It is php and using mysql for the database commands. Commented Jul 21, 2014 at 16:18

1 Answer 1

1

You're mixing up two vars.

$campaign[$campaignCalls] += 1;

should be

$campaignCalls[$campaign] += 1;

since $campaignCalls is the array and $campaign the index.

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

Comments

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.