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

I have an error in my code, can you detect it, and I don't understand about row() can someone explain. Here is my code-
$sqlstatement=qq~ SELECT Tarikh, StatusType, ProblemCategory, SubProblem, Pr +oblemSolution FROM daftarstatus WHERE ICNumber = "$ICNumber" AND CompanyRegistrationNumber = "$CompanyReg +istrationNumber" ~; $sth = $dbh_base_app->prepare($sqlstatement); $sth->execute() or &error("SQL","Couldn't execute statement: $DBI: +:errstr"); if($sth2->rows() <= 0) { $sth2= $dbh_base_app->prepare($sqlstatement2); $sth2->execute() or &common_error("$sqlstatement2","Couldn't execute s +tatement1: $DBI::errstr",1); $number = 0; while(($Tarikh, $StatusType, $ProblemCategory, $SubProblem, $ProblemS +olution) = $sth2->fetchrow_array()) { $number++;
The error is " Can't call method "rows" on an undefined value at...."

Thanks

Replies are listed 'Best First'.
Re: Don't understand row
by sgifford (Prior) on Feb 14, 2005 at 04:51 UTC
    Probably one of your prepare statements is failing. Check the return values, and see what error you get:
    $sth = $dbh_base_app->prepare($sqlstatement) or &error("SQL","Couldn't execute statement: $DBI::errstr");
Re: Don't understand row
by Zaxo (Archbishop) on Feb 14, 2005 at 04:58 UTC

    Is $sth2 supposed to be spelled $sth? If not, you need to show some different code.

    In general, you should be using placeholders instead of interpolating values into the SQL text. You are certain to run into quoting difficulties without them. That could be another source of trouble for you.

    The rows method of a statement handle is not really meant to be used with select. Expect odd results.

    After Compline,
    Zaxo

      Ok, It was supposed to be $sth. But I don't understand this statement "if($sth->rows() <= 0)".

      After fixing all those simple error, I add this code to test it "else{...." and the output is from the else{... where it should be from the if(.....

      sorry for my english.

      Thanks

        DBI's rows method is supposed to return the number of rows affected by the sql statement after it is executed. That is only meant to work with table-modifying statements, but some drivers may return good results for selects as well.

        That you are seeing the comparison fail means you are getting a positive number from rows. That is what you would expect for a successful query, assuming select $sth's rows works.

        After Compline,
        Zaxo

Re: Don't understand row
by murugu (Curate) on Feb 14, 2005 at 04:53 UTC

    Hi,

    Have u already defined $sth2?

    If not, the Problem is you are calling rows method using undefined object $sth2. You have mentioned $sth upto the execution part and you are using $sth2 thereafter.

    Regards,
    Murugesan Kandasamy.

Re: Don't understand row
by K_M_McMahon (Hermit) on Feb 14, 2005 at 05:01 UTC
    Update: Damn, There were no replies when I started this post. Now there are 3 before mine! You guys are fast!

    AnonyMonk: The error is " Can't call method "rows" on an undefined value at...."
    Have you intialized $sth2? The error means you are trying to execute a module call on an object you have not yet created.

    You have:
    $sth = $dbh_base_app->prepare($sqlstatement);
    where you defined $sth....but you don't define $sth2 as an object of $dbh_base_app
    You don't tell us which type of database you are interacting with, but the MySql/Perl documentation is here.