Ramprasad A Padmanabhan wrote:


Voodoo Raja wrote:

Sorry for not posting it in the first place.

Will this sub.. chew memory if I run it a number of times.. or does it overwrite it.

sub read {
print "\nreading data";
$db = DBI->connect("DBI:mysql:$dbase:$hostname", $username, $password);

as Ramprasad said, you should pass in your db connection if you use it in more than one function...


$query = $db->prepare("SELECT * FROM cash where f1 = '1002'");

Generally speaking, if you're selecting " * " then you're doing something wrong. You should only select exactly what you want from a database rather than "just give me it all and I'll figure it out later". Especially because--judging from the loop below--you don't appear to use anything in the db except the first field, but also because this method doesn't take into account changing database structure. Ramprasad suggested "select max($field1) from cash where $x = $y " but keep in mind you should do this using placeholders where any user input is involved to protect yourself from SQL injection attacks. =]


$query->execute;
$numrows = $query->rows;
while (@array = $query->fetchrow_array ) {
($field1, $field2, $field3, $field4, $field5, $field6, $field7, $field8, $field9, $field10,
$field11, $field12, $field13, $field14, $field15, $field16, $field17, $field18) = @array;}
$query->finish;
$db->disconnect;

Don't disconnect yourself from the database in the middle of a while loop that's iterating through database query results!
Move finish and disconnect out of the loop.



print "\nf1 is $field1"; $lasttrans = "$field1"; $newtrans = ($lasttrans+1); }

[...]


What you seem to be doing is not very clear. Why are you running this loop
while (@array = $query->fetchrow_array ) {
($field1, $field2, $field3, $field4, $field5, $field6, $field7, $field8, $field9, $field10,
$field11, $field12, $field13, $field14, $field15, $field16, $field17, $field18) = @array;}


If you just want the last  transaction id  change the query
to "select max($field1) from cash where $x = $y ";

This will give you the last transaction id directly


BTW 1) Have you considered using an autoincrement field
2) Do not connect to the database in every function of yours .
Make one connection in the beginning and pass the handle to all functions.
3) And when you post a reply to a newsgroup post your reply *at the bottom* avoid top posting


Ram








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



Reply via email to