1

Suppose you have this:

while ($record = mysql_fetch_assoc($dbquery)) {
    $arr[] = $record['$columnvalue1'];
}
$arraycount = count($arr);

and you want multiple columns in the array how is this done?

while ($record = mysql_fetch_assoc($dbquery)) {
    $arr[] = array($record['$columnvalue1'],$record['$columnvalue2']);
}
$arraycount = count($arr);

the above code errors:

Warning: array_count_values(): Can only count STRING and INTEGER values! 
8
  • 1
    why not just $arr[] = $record? Commented Apr 20, 2012 at 5:16
  • 1
    if you want multiple fields in array use like $arr['field1'] = $record['field1']; $arr['field2']=$record['field2'] .. Commented Apr 20, 2012 at 5:18
  • I don't see an issue with your design, but I don't know what kind of data you're passing in, either. Commented Apr 20, 2012 at 5:20
  • 5
    You are leaving something out. The error is for a function not in your sample code. Commented Apr 20, 2012 at 5:20
  • 2
    Please stop writing new code with the ancient mysql_* functions. They are no longer maintained and community has begun the deprecation process . Instead you should learn about prepared statements and use either PDO or MySQLi. If you care to learn, here is a quite good PDO-related tutorial. Commented Apr 20, 2012 at 5:27

1 Answer 1

1

To add multiple column values in to an array

$arr = array();
while ($record = mysql_fetch_array($dbquery)) {
    array_push($arr,$record['$columnvalue1'],$record['$columnvalue2']);
}

To find the sum of the array

$arraycount = array_sum($arr); //to find the sum of the array
$num = sizeof($arr); //to find the size of the array
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.