Database query

Hello,

Basically I am trying to query database and keep count of records that have the same data in the campaign field.

Step one gather
[php]<?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 campaign is in array, if so increment by 1
	  if (array_key_exists($campaign, $campaignCalls)){
		  $campaign[$campaignCalls] += 1;
	  }
	  //Add key and value to array after determining key is not in array
	  else {
		  $campaignCalls[$campaign] = 1;
	  }
	  }
	mysqli_close($con);
    ?>[/php]

Here is the error I am getting

Fatal error: Cannot use assign-op operators with overloaded objects nor string offsets in on line 18

You are going about it all wrong. All you need to do is add a WHERE clause to your query.

SELECT COUNT (1) FROM phone_records WHERE campaign =‘dataYouWant’

Get rid of all that array keys stuff.

If you insist on doing your way, you have to figure out if you want $campaign to be an array or a $key, but you can’t have both. :wink: However, Kevin has the better solution.

Robert,

There is around 70 different types of calls ($campaign) that I am trying to get a count of each.

Either I am not understanding where you are going with that, or I did not explain as well.

Strider64, Thanks. After seeing that mistake I was able to get the code to work properly.

You are still over coding. All you need to do is add the WHERE clause to get your count.

Thanks guys, I am still pretty new to programming and appreciate you help. I am not insisting on doing it any particular way. This was the only solution that I could come up with for my needs.

Sponsor our Newsletter | Privacy Policy | Terms of Service