I have an empty table which is called 'distinct'. Thiw table have some columns with name 'wid', 'variables' and 'timestamp'... I have the following code :
// Begin building the query.
$query = db_select('watchdog', 'wa');
$query->join('distinct', 'd', 'wa.wid = d.wid');
$query->fields('wa', array('variables', 'wid', 'timestamp', 'type', 'message',
'severity'))
->range(0, 20);
$result = $query->execute();
// Loop through each item and add to $row.
foreach ($result as $row) {
$delete_old_rows = db_delete('distinct')
->condition('variables', $variables, '=')
->condition('timestamp', $timestamp, '<')
->execute();
$select_same_variables = db_select('distinct', 'd')
->fields('d')
->condition('variables', $variables, '=')
->execute();
if ($select_same_variables->rowCount() == 0) {
$query = db_insert('distinct')
->fields(array('timestamp' => $timestamp, 'wid' => $wid,
'variables' => $variables,
))
->execute();
}
When i run this code without join all goes perfect but it runs only for 20 values.. When i run it with join not one time goes in the 'for' loop... why i have this problem?
Distinct, Timestamp & Type all sound like they might be reserved words in a lot of different databases. It is good practice to never name a column (or a table) using a reserved word even if your database permits it.
As for the loop only runs 20 times, it is probably due to some unexpected data (null value for timestamp perhaps) that is not present until the 20th iteration.