Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

using has_selected_record() of Net::MySQL

by ranjan_jajodia (Monk)
on Aug 11, 2005 at 07:31 UTC ( #482863=perlquestion: print w/replies, xml ) Need Help??

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

Hi Monks,
I have written a script which checks if an id is already present in the database and depending on that it does other things. Following is the part of the code where i have confusion:
$mysql->query(q{select id from id_table where id = '1'}); if ( $mysql->has_selected_record) { #do something; } else { #do something else }
In the above 'if loop', the condition always return true even if the id is not present in the table. Am i misunderstanding the use of 'has_selected_record()'? If yes how do i know whether the query has returned result or not? I understand I can create an iterator and get the exact result but i don't think that is needed because
1) I don't want the result- I just want to know whether there is a result or not.
2) The query will be excuted a lot of times and creating iterator each time will not be resource friendly.
I have gone throught the documentation present at CPAN along with the module Net::MySQL but unfortunately did not get much info. Could you please clear my doubt?

Regards,
Ranjan

--Let Logic Rule--

Replies are listed 'Best First'.
Re: using has_selected_record() of Net::MySQL
by critter (Acolyte) on Aug 11, 2005 at 11:54 UTC
    Hi,

    Maybe you can check on how many records you get returned. The CPAN pages mentions the next function:

    my $affected_rows = $mysql->get_affected_rows_length;

    I'm guessing that the has_selected_record returns always true because it can be run and will always return a value. I've never used this module but you could check and see what you get as return value. If this is a number you could change your check a bit perhaps to:

    if ( $mysql->has_selected_record > 1 ) { #do something; } else { #do something else }

    Just my 2 cents,

    Grtz,

    Critter

      Hi,
      get_affected_rows_length() will work only in case of 'update queries'. In 'select queries it will always return zero.
      has_selected_record() returns true or false as written in the specifications at CPAN.

      Thanks for the effor though :-),
      Ranjan

      --Let Logic Rule--

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (3)
As of 2023-09-26 20:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?