?
Solved

How to Loop through a recordset with a counter

Posted on 2008-06-22
3
Medium Priority
?
1,252 Views
Last Modified: 2012-06-27
I am totally new to php, and after hours cannot figure this out.
I have this code (sample #1) to get a recordset that has one column, and loop through it. It work find
My problem is that I must build a moving average based on the values in the recordset.  Do do so, I want to loop through the recordset using a counter like in Sample #2

 
Sample #1
$rsMA = odbc_exec($conn,$sql);
while($row = odbc_fetch_array($rsMA))
{echo $row['Close'];}
Sample #2
for($i=1; $i < 200; $i++) {
  	echo $row[$1];
}

Open in new window

0
Comment
Question by:rrhandle8
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 11

Expert Comment

by:spoxox
ID: 21843350
See $count and $tally in the snippet.
<?php 
//connect to MySQL
  $username = "eeuser";
  $password = "eepw";         
  $hostname = "localhost";
  $dbconn   = mysql_connect($hostname, $username, $password) or die("Unable to connect to MySQL");
 
//connect to one MySQL database
  $database = mysql_select_db("eedatabase",$dbconn);
 
//text of the query to execute against the database 
  $query    = "SELECT aKey, aName, anAmount FROM aTable";
 
//execute the query, store the result set
  $result   = mysql_query($query) or die(mysql_error());
 
  $count = 0;
  $tally = 0;
//access all the data, one row at a time
  while ($row = mysql_fetch_array($result)) {
    echo $row["aKey"] . ' '. $row["aName"];
    $count++;  // will count each row
    $tally += $row["anAmount"];  // will add up all anAmount fields
    
  }
 
   mysql_close($dbconn);
?>

Open in new window

0
 

Author Comment

by:rrhandle8
ID: 21843384
ERROR:
Warning: mysql_fetch_array(): supplied resource is not a valid MySQL result resource

I am using ODBC to connect to an Access database.  Does that make a difference?
0
 
LVL 11

Accepted Solution

by:
spoxox earned 375 total points
ID: 21846110
Yes; sorry, didn't pay attention to that. The mysql_* stuff is particular to MySQL. Just replace it with the appropriate ODBC stuff, something like the attached snippet.
//something like this SQL:
$sql = "select Close, anAmount from someTable";
 
$rsMA = odbc_exec($conn,$sql);
 
$count = 0;
$tally = 0;
 
while($row = odbc_fetch_array($rsMA)) {
  echo $row['Close'];
  $count++;  // will count each row
  $tally += $row["anAmount"];  // will add up all anAmount fields
}

Open in new window

0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question