Loading ...
Sorry, an error occurred while loading the content.

MySql: How to find whether any rows affected or not?

Expand Messages
  • Vijaya Kumar
    Dear Friends, $sth-rows() returns affected rows 1 even if there is no change in the database while update. How do I get 0 when there is no change in the
    Message 1 of 1 , Jul 21 6:11 AM
    • 0 Attachment
      Dear Friends,

      $sth-rows() returns "affected rows 1" even if there is no change in
      the database while update. How do I get "0" when there is no change
      in the database?

      Any help would be appreciated.

      The following is my code.

      use strict;
      use warnings;
      use DBI;
      my($dbi);

      DBConnect("localhost", "", "test", "root", "root");

      my $sql;
      $sql = "update mydb set textfield=\"sometext\" where id=\"1\"";

      my ($sth);

      $sth = $dbi->prepare($sql);
      $sth->execute();

      print "\n affected rows:", $sth->rows();

      sub DBConnect
      {
      my($host,$port,$database,$user,$passwd) = @_;
      my $dsn
      = "DBI:mysql:$database:$host:$port:mysql_client_found_rows=0";
      $dbi = DBI->connect($dsn,$user,$passwd, {AutoCommit => 1,
      RaiseError => 1}) || die return DBI->errstr;
      }

      Regards
      Vijay
    Your message has been successfully submitted and would be delivered to recipients shortly.