Al_
Al_

Reputation: 1511

Is there a way to retrieve the output of SHOW WARNINGS with Zend_Db_Adapter?

Let's consider a very basic table:

CREATE TABLE test_warning (col_a INT NOT NULL, col_b INT NOT NULL)

+-------+---------+------+-----+---------+-------+
| Field | Type    | Null | Key | Default | Extra |
+-------+---------+------+-----+---------+-------+
| col_a | int(11) | NO   |     | NULL    |       |
| col_b | int(11) | NO   |     | NULL    |       |
+-------+---------+------+-----+---------+-------+

When I insert a row not specifying one of the columns a warning is generated:

INSERT INTO test_warning (col_a) VALUES (1);
Query OK, 1 row affected, 1 warning (0.05 sec)

SHOW WARNINGS;
+---------+------+--------------------------------------------+
| Level   | Code | Message                                    |
+---------+------+--------------------------------------------+
| Warning | 1364 | Field 'col_b' doesn't have a default value |
+---------+------+--------------------------------------------+

Has anyone tried to retrieve the output of SHOW WARNINGS using Zend_Db_Adapter in a zend application?

I tried following:

I'm starting to believe that Zend_Db_Adapter is "only" useful to retrieve data via SELECT.

Upvotes: 1

Views: 176

Answers (1)

jmat
jmat

Reputation: 320

Interesting question. Ive just run a test through and fetchAll() does seem to return the results of MySQL warnings, at least for me! I'm running ZF 1.12.0

Firstly I ran an SQL statement on the command line where the value of a field exceeded the VARCHAR limit and where a column didn't have a default value.

Here are the results:

mysql> INSERT INTO `myTable` (`example2`) VALUES ('a value that is too long');

mysql> show warnings;
+---------+------+-------------------------------------------------+
| Level   | Code | Message                                         |
+---------+------+-------------------------------------------------+
| Warning | 1364 | Field 'example1' doesn't have a default value   |
| Warning | 1265 | Data truncated for column 'example2' at row 1   |
+---------+------+-------------------------------------------------+

I then ran the same query using Zend_Db...

$stmt = $db->query(
    'INSERT INTO `myTable` (`example2`) VALUES (\'a value that is too long\')'
);


$stmt = $db->query(
    'SHOW WARNINGS;'
);

$result = $stmt->fetchAll();

echo '<pre>';
var_dump($result);
echo '</pre>';

And here are the results:

array(4) {
  [0]=>
  array(3) {
    ["Level"]=>
    string(7) "Warning"
    ["Code"]=>
    string(4) "1364"
    ["Message"]=>
    string(44) "Field 'example1' doesn't have a default value"
  }
  [1]=>
  array(3) {
    ["Level"]=>
    string(7) "Warning"
    ["Code"]=>
    string(4) "1265"
    ["Message"]=>
    string(44) "Data truncated for column 'example2' at row 1"
  }
}

The main difference here is that i'm writing the SQL directly into the query() method rather than constructing it using Zend_db_Select. It could be worth giving that a shot, though in theory it shouldn't make a difference.

Upvotes: 1

Related Questions