Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

datatype from SQL

by jck (Scribe)
on Jun 23, 2006 at 17:05 UTC ( [id://557260]=perlquestion: print w/replies, xml ) Need Help??

jck has asked for the wisdom of the Perl Monks concerning the following question:

this seems simple, but i'm running into confusion: i have the following very simple SQL statement:
my $sql = "SELECT UID FROM news";
and then the following loop to match the requested UID with the results of the query, and identify the previous and next records:
while (my $newUID = $sth->fetchrow) { if ($in{'requestID'} == $newUID) { $currID = $newUID; } else { $prevID = $newUID; } ... #there's a counter to generate one more loop after the currID is f +ound, to capture "nextID" $nextID = $newUID; } print p(["Previous = |$prevID|","Current = |$currID|","Next = |$nextID +|"]);
so, if i submit the value 2 as 'requestID', the results for $prevID is correctly listed as 1, $currID is undefined, and $nextID gets assigned as 2. this is what it looks like:
0 #this is a counter value to escape the while loop after finding the +value of $nextID Previous = |1| Current = || Next = || 1 Previous = |1| Current = || Next = |2|

it seems to me that the condition ($in{'requestID'} == $newUID) is not getting evaluated correctly, and i'm wondering if one of the other value is not recognized as an integer? is that the problem? how do i fix it? the database file is MySQL, and the UID field is defined as an integer field.

TIA, janaki

Replies are listed 'Best First'.
Re: datatype from SQL
by VSarkiss (Monsignor) on Jun 23, 2006 at 17:23 UTC
      well, i tried your suggestion, changing $newUID to $newUID->[0], but it gave me the following error:
      Can't use string ("1") as an ARRAY ref while "strict refs" in use at n +ewsedit.pl line 61.
      so i think that $newUID must be the scalar ref, not an array ref, right?

        What version of DBI are you using and which DBD driver? You're use of $in{param} leads me to believe you're also using cgi-lib (instead of CGI) and may have a very old installation.

        -derby

        Update: And is this an XY Problem ... are you just trying to get create the next UID ... if so, most DB's have an auto-incrementing field feature.

      i thought that might be the problem, but then, why does
      print $newUID
      give me the value "2"?? (or "1", or whatever)??
      i think i may have had the whole fetchrow_arrayref, now it's fetch...i may have cut & pasted an intermediate incorrect version.

      and, yes, i am using DBI

Re: datatype from SQL
by jck (Scribe) on Jun 23, 2006 at 17:49 UTC
    OK, well, it was really stupid!!! so sorry to take up anyone's time (and thank you VSarkiss for tryine!!)....it turned out that my loops were poorly constructed, and the condition that i thought was failing was never getting tested at all.

    it works fine now

    just had to work through it a little

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://557260]
Approved by VSarkiss
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (3)
As of 2024-04-24 01:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found