Hello Monks,

I am using "use DBI" on perl v5.8.8 to update MySQL database table.

Statement I am using is like this:

my $sql6 = "UPDATE table_name SET good_high = $threshold_good_high, c +ritical_low = $threshold_critical_low, warning_low = $threshold_warni +ng_low, warning_high = $threshold_warning_high WHERE user = ? AND nam +e = '$threshold_name'"; my $sth6 = $dbh->prepare($sql6); $sth6->execute($userName); $update_result1 = $sth6->rows; print "\$update_result1: $update_result1\n";

Here I am actualy trying to get number of rows changed by this update command (which will vary from 0 to 29 in my case). But rows is always giving me 29 as result even if no row was changed. Which I think is expected as per documentation, but I didn't understand it 100%.

Per documentation of DBI: Returns the number of rows affected by the last row affecting command, or -1 if the number of rows is not known or not available. Generally, you can only rely on a row count after a non-SELECT execute (for some specific operations like UPDATE and DELETE), or after fetching all the rows of a SELECT statement.

So, I tried using:

my $sth6 = $dbh->do("UPDATE table_name SET good_high = $threshold_good +_high, critical_low = $threshold_critical_low, warning_low = $thresho +ld_warning_low, warning_high = $threshold_warning_high WHERE user = ? + AND name = '$threshold_name'"); print "\$sth6: $sth6\n";

UPDATE(discard):But in the above case it prints nothing as $sth6

UPDATE(consider):But in the above case ALSO it prints 29 as $sth6. I had missed providing $userName earlier.

Can you please suggest me what am I missing here and a way to get count of rows changed by UPDATE command.


In reply to [Solved]: How to get count of rows changed by MySQL UPDATE command using DBI by Perl300

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.