spewn
Programmer
- May 7, 2001
- 1,034
here's my code:
pretty basic. the code gets all the prodID's from the table 'products'. then it prints the name, type and price of each prodID instance. no prob. but how do i count how many prodID's are returned from the query? as you see, i have a variable $srchCount that increments each time it runs through the code...but this only gives me an accurate value AFTER the code is run. i need to know ahead of time.
i tried adding an additional "while() {}" statement and re-arranged the location of $srchCount increment:
<---
my $addVar=1;
$srchCount=0;
while (@row=$sth->fetchrow_array){$srchCount++}
while (@row=$sth->fetchrow_array){
my $sql1 = "select name from products where prodID='@row'";
--->
but then my second while() loop wasn't even read, or wasn't read right because there was no output result to the browser. it's like the script didn't like two while() loops.
i'm looking for a solution here.
also, is there a simplified way to query my 3 fields (name, type and price), or is the way i have it correct/efficient? for instance, what if i have to get values from 10 fields? just have ten separate queries?
one last thing...is $sth->fetchrow_array the best to use if the result is only 1 value?
thank you.
- g
Code:
my $dbh = DBI->connect('dbi:mysql:gtc','','');
my $sql = "select prodID from products";
my $sth = $dbh->prepare($sql);
$sth->execute;
my $addVar=1;
$srchCount=0;
while (@row=$sth->fetchrow_array){
my $sql1 = "select name from products where prodID='@row'";
my $sth1 = $dbh->prepare($sql1);
$sth1->execute;
$str1 = $sth1->fetchrow_array;
my $sql2 = "select type from products where prodID='@row'";
my $sth2 = $dbh->prepare($sql2);
$sth2->execute;
$str2 = $sth2->fetchrow_array;
my $sql3 = "select price from products where prodID='@row'";
my $sth3 = $dbh->prepare($sql3);
$sth3->execute;
$str3 = $sth3->fetchrow_array;
print << "EOF";
<tr valign=top>
<td align=center valign=top>Name: $str1</td>
<td align=center valign=top>Type: $str2</td>
<td align=center valign=top>Price: $str3</td>
</tr>
EOF
$addVar++;
$srchCount++;
}
pretty basic. the code gets all the prodID's from the table 'products'. then it prints the name, type and price of each prodID instance. no prob. but how do i count how many prodID's are returned from the query? as you see, i have a variable $srchCount that increments each time it runs through the code...but this only gives me an accurate value AFTER the code is run. i need to know ahead of time.
i tried adding an additional "while() {}" statement and re-arranged the location of $srchCount increment:
<---
my $addVar=1;
$srchCount=0;
while (@row=$sth->fetchrow_array){$srchCount++}
while (@row=$sth->fetchrow_array){
my $sql1 = "select name from products where prodID='@row'";
--->
but then my second while() loop wasn't even read, or wasn't read right because there was no output result to the browser. it's like the script didn't like two while() loops.
i'm looking for a solution here.
also, is there a simplified way to query my 3 fields (name, type and price), or is the way i have it correct/efficient? for instance, what if i have to get values from 10 fields? just have ten separate queries?
one last thing...is $sth->fetchrow_array the best to use if the result is only 1 value?
thank you.
- g