I am developing a dynamic web page using Drupal 7. I ran into a very strange problem. I have reduced my problem to a very small test case as follows:

The database side: I am using MySQL. I have two tables as defined here with a few sample data entries:

CREATE TABLE people (
  pid INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
  name VARCHAR(30) NOT NULL
);
INSERT INTO people VALUES (NULL, 'Joe');
INSERT INTO people VALUES (NULL, 'Ant');
INSERT INTO people VALUES (NULL, 'Tom');

CREATE TABLE event (
  eid INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
  pid INT(6) UNSIGNED NOT NULL,
  event_desc VARCHAR(20) NOT NULL,
  event_date DATE NOT NULL
);
INSERT INTO event VALUES (NULL, 1, '1p', '2015-10-01');
INSERT INTO event VALUES (NULL, 2, '1p', '2015-10-12');
INSERT INTO event VALUES (NULL, 2, '2p', '2015-10-00');
INSERT INTO event VALUES (NULL, 2, '3p', '2015-00-00');
INSERT INTO event VALUES (NULL, 3, '1p', '2010-07-18');
INSERT INTO event VALUES (NULL, 3, '2p', '2010-09-00');

Note the only interesting feature here is that the event table may contain incomplete event date in the format of '2015-10-00' when date is unknown and '2015-00-00' when both date and month are unknown. I understand this is legal in MySQL.

The Drupal 7 side: I create a custom module as follows:

<?php
/**
 * Implements hook_menu().
 */
function test_menu() {
  $items['test'] = array(
    'type' => MENU_NORMAL_ITEM,
    'title' => 'Test',
    'description' => 'Test',
    'page callback' => '_test_page',
    'access callback' => TRUE,
    'menu_name' => 'main-menu',
  );
  return $items;
}

function _test_page() {
  $output = "";
  $sql = "SELECT event.event_desc, event.event_date, people.name FROM  event, ";
  $sql .= "people WHERE event.pid=people.pid ORDER BY people.name";
  $result = db_query($sql);
  foreach ($result as $data) {
    $output .= $data->name. " ". $data->event_desc. " ". $data->event_date. "<br>\n";
  }
  return $output;
}

Note that I create a page with a menu item 'Test'. The page performs a query and displays the result. Now the very strange part: when the event date is incomplete, the query returns the wrong data '0000-00-00'. I am expecting this result:

Ant 2p 2015-10-00
Ant 3p 2015-00-00
Ant 1p 2015-10-12
Joe 1p 2015-10-01
Tom 1p 2010-07-18
Tom 2p 2010-09-00

I am getting this INCORRECT result:

Ant 1p 2015-10-12
Ant 2p 0000-00-00
Ant 3p 0000-00-00
Joe 1p 2015-10-01
Tom 2p 0000-00-00
Tom 1p 2010-07-18

I tries to isolate the problem. Here are my observations:
- My SQL statement appears to be correct. I ran it direct from mysql command line to get the correct result.
- I wrote a standalone PHP code and pass my query directly to PDO and I again got the correct result.
- The error only occurs when I do a join in my query. If accessing the 'event' table only I can get those correct dates returned.
- Given the above, is there a problem in Drupal 7's database abstraction layer?

Comments

Jaypan’s picture

Maybe try it using an actual JOIN between the tables on the pid column.

macelee’s picture

As I explained in the original post, this is not a problem with my SQL statement because the same query works well if I do it directly on mysql command line, or through a standalone PHP page. I tried to reformulate the query using a LEFT JOIN anyway - I still experience the same problem.

Jaypan’s picture

But you're not doing it through the command line or on a standalone page, you're doing it through Drupal with PDO, which is why I gave you an alternative to try to see if maybe the bug doesn't show when you do it that way.

macelee’s picture

Thanks for your clarification. I already did what you told me to do. When using a LEFT JOIN to reformulate my query I still get the same wrong result. I also tried to process the result set in different ways (fetching record as object or associated array etc) but the incorrect result appears to be in the result set already.

Jaypan’s picture

Too bad (that it didn't fix the problem).

That does seem to be quite random. But it also appears that the DATE type is not supported by the Drupal Schema API: https://www.drupal.org/node/159605

I personally always use UNIX timestamps for dates in Drupal (as does core). If you can't find a solution around this, you may consider changing to UNIX timestamps.

macelee’s picture

Thanks for your help. Of course there are ways around this. I just converted the column for DATE type to VARCHAR. My query can return the desired result now. Of course I lose the ability to use any DATE related functions which isn't important for my application.