Advertisement

05.24.2008 at 07:16PM PDT, ID: 23430870
[x]
Attachment Details

An UPDATE gives warnings, but mysql_warning_count is empty.

[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

8.0
Zone:

MySQL Server

Hi,
I'm trying to see mysql warnings as they happen,with mysql being called from perl programs.

From this URL:
http://use.perl.org/~Smylers/journal/34246
I found the following code:
-----begin quote from use.perl.org-----------
use Lingua::EN::Inflect qw<inflect>;
my $insert = $db->prepare(q[INSERT INTO game SET shape = 'rock']);
$insert->execute;
if ($insert->{mysql_warning_count})
{
  warn inflect "NUM($insert->{mysql_warning_count}) PL_N(warning):\n";
  my $warning_query = $db->prepare(q[SHOW WARNINGS]);
  $warning_query->execute;
  while (my $warning = $warning_query->fetchrow_hashref)
  {
    warn "$warning->{Message}\n";
  }
}
which yields this output:
1 warning:
Data truncated for column 'shape' at row 1
------end quote from use.perl.org------------

So I made a couple of slight modifications, and came up with this for my program (the field "Cancel_date" is a date field):

$sth = $dbh->prepare ("UPDATE my_users SET Cancel_date='lskdifjlkijef' WHERE id_number=33");
$numUpdated = $sth->execute;
if ($sth->{mysql_warning_count})
{
  my $warning_query = $dbh->prepare(q[SHOW WARNINGS]);
  $warning_query->execute;
  while (my $warning = $warning_query->fetchrow_hashref)
       {print STDERR "$warning->{Message}\n"}
}
else
{print STDERR "no warnings\n"}

I expected to see the warning "Data truncated for column 'Cancel_date' at row 1" which is the warning that I see if I run the poorly-formed update statement from a telnet mysql command line.
But I get "no warnings" instead.

I finally tracked it down to the initial "if" statement.  $sth->{mysql_warning_count} is empty in this line:
if ($sth->{mysql_warning_count})
... so I don't get the warnings printed to STDERR.  If I leave out that opening "if" statement and instead just run this code:
$sth = $dbh->prepare ("UPDATE my_users SET Cancel_date='lskdifjlkijef' WHERE id_number=33");
$numUpdated = $sth->execute;
  my $warning_query = $dbh->prepare(q[SHOW WARNINGS]);
  $warning_query->execute;
  while (my $warning = $warning_query->fetchrow_hashref)
       {print STDERR "$warning->{Message}\n"}

... then the expected warning gets printed to STDERR just fine.

I'd like to have one "if" statement that will tell me whether there are any warnings, so if there are, I can process them in the block below the "if" statement, and if there are no warnings, I don't have to do the processing in the block.  Seems like mysql_warning_count is tailor made for this, but it's not working for me.

What am I doing wrong here -- why can't I get mysql_warning_count to work?
 
Assisted Solution by angelIII:

All comments and solutions are available to Premium Service Members only. Start your 7-day free trial to view the solution to this question.

Already a member? Login to view this solution.

 
 
Accepted Solution by StevenMiles:

All comments and solutions are available to Premium Service Members only. Start your 7-day free trial to view the solution to this question.

Already a member? Login to view this solution.

 
 
20081119-EE-VQP-45 / EE_QW_2_20070628