CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: DBI:
numbers in mysql

 



jo
New User

Jul 28, 2003, 5:20 AM

Post #1 of 3 (2982 views)
numbers in mysql Can't Post

I have a table with amongst other things a code column. I then have a Perl script that looks into the code column and automatically produce a new code that is simply the last code in the database +1.



The snippet of code in Perl is as follows:

while (@vresult=$v->fetchrow())

{ for($index=0;$index<@vresult;$index++)

{$c=$vresult[$index];

}

}

$code=$code+1

@vresult is the result of a database query to get the code out of the database.

it works fine when I have code 1-9 in the database but as soon as I have code above 10 then the code doesn't work anymore, it keeps wanting to print $code=10.

I realised that is because it considers 9 as being a higher number than 10. So my question is how can I improve this perl code so that if in the databse I have a code 11 the $code will become 12 and not 9?



Thank you


localfilmmaker
stranger

Feb 5, 2004, 10:30 AM

Post #2 of 3 (2833 views)
Re: [jo] numbers in mysql [In reply to] Can't Post

Mmm. What data type is you column for 'code'? The way you are describing it, it sounds like it is not an integer. I would recommend changing it to an integer and then most of your problems will go away.
On another note: is this code column the primary key for your table? If it is you should make it an auto_increment field too. That way MySQL will do all the incrementing for you, so you never have to set this value in your perl code.

If these suggestions are not an option, then try this code:

Code
#-- Grab the highest value of 'code' in the database 
my ($code) = $dbh->selectrow_array("SELECT MAX(CAST(code AS SIGNED)) FROM your_table");
#-- Add one to it
$code += 1;

-localfilmmaker
Spencer Christensen


davorg
Thaumaturge / Moderator

Feb 5, 2004, 11:42 PM

Post #3 of 3 (2830 views)
Re: [jo] numbers in mysql [In reply to] Can't Post

We'd need to see more of the code to be sure, but it sounds like you are using the string comparison operators (eq, gt, le, etc) when you really want the numeric comparison operators (==, >, <=, etc).

--
Dave Cross, Perl Hacker, Trainer and Writer
http://www.dave.org.uk/
Get more help at Perl Monks

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives