Perl / MySQL problem

2002-05-14 Thread FLAHERTY, JIM-CONT

Does anyone have an Idea whats going on here  , I have an array feeding a
SQL Query, But it doesnt always come back with a success .  What this query
does it is on a Quiz web site . Students take quiz, variable is passed to
grade.cgi. that variable is parsed with a while statement. and a variable is
feed to the query. If the student takes 10 question, only 6 or 7 will get
graded. 
 
I have red hat 7.2 and mysql 3.23  , is it possible the while statement
executes to fast for the query ??  
 
Code:
##
#  connect to DB

use DBI;
my $database = cram;
my $data_source = DBI:mysql:$database;
my $username = user;
my $password = password;
$dbh =DBI -connect($data_source, $username, $password) or die cant connect
to
$data_source : my $dbh- errstr\n;
 

  while( $questions_asked =~ /!([^!]*)/g) {
 
  my $num1 = $1;
 
 
print  This is a test:  $num1BR;   # this statement for debug
 
my $sth1 = $dbh - prepare(select * from tests where num  = '$num1' );
 
$sth1 - execute or die  unable to execute query ;
 
#$sth1 - finish;
 
my $array_ref = $sth1 - fetchall_arrayref();
 
 
 
 
 
It would miss every other to every third one Help!
thanks
Jim 
 



Re: Perl / MySQL problem

2002-05-14 Thread Felix Geerinckx

on Tue, 14 May 2002 18:57:56 GMT, Jim-Cont Flaherty wrote:

   while( $questions_asked =~ /!([^!]*)/g) {
  
   my $num1 = $1;
  
  
 print  This is a test:  $num1BR;   # this statement for debug
  
 my $sth1 = $dbh - prepare(select * from tests where num  = '$num1'
 ); 
  
 $sth1 - execute or die  unable to execute query ;
  
 #$sth1 - finish;
  
 my $array_ref = $sth1 - fetchall_arrayref();

Change the prepare to 

my $sth1 = $dbh-prepare(select * from tests where num  = ?);

and move it out of the while loop.

Change you execute statement into

$sth1-execute($num1) or die  unable to execute query ;


Don't 'finish' before you have fetched.

-- 
felix

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]