ARRAY Question ...

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
nwoutback
Forum Newbie
Posts: 17
Joined: Mon Nov 24, 2003 1:50 pm

ARRAY Question ...

Post by nwoutback »

I'm using the below code to pull all columns from a table and list the column names and SUM totals of their content ... I've got 8 distributor records in this table, and I'd like to pull columns and SUMS of, per distributor ... How?

Code: Select all

<?php
$connection=mysql_connect("localhost","user","pw") or die('Could not connect to the database server'); 
$db = mysql_select_db("tanaka-ism_com", $connection) or die ("Unable to select database."); 

$columns = array("AST210","AST210Sales","TBC2251","TBC2251Sales"); 

foreach($columns as $columns) 
{ 
$query = "SELECT SUM($columns) FROM distreport03 WHERE RptDist = 'Gardner'"; 
$mysqlresult = mysql_query($query); 
$result = mysql_fetch_array($mysqlresult); 
echo "{$columns} : {$result[0]}<br />"; 
mysql_free_result(); 
} 
?>
User avatar
m3mn0n
PHP Evangelist
Posts: 3548
Joined: Tue Aug 13, 2002 3:35 pm
Location: Calgary, Canada

Post by m3mn0n »

I've never seen results pulled like that before (the query within the loop). If I'm not mistaken, doesn't that seriously slow down script execution time?

I suggest sticking to the ways shown on the function pages here: [php_man]mysql[/php_man].

The answer to your question also lies there. :)
Post Reply