I have the following sql query
SELECT * FROM jos_jcalpro_events AS e
LEFT JOIN jos_jcalpro_events_seats AS s ON e.extid = s.extid
LEFT JOIN jos_jcalpro_events_types_xref AS t ON e.extid = t.extid
which I execute via PDO from PHP. I create out of this another query to reinsert in the database. But it doesn’t work. When I change LEFT from upper to lower case it does:
SELECT * FROM jos_jcalpro_events AS e
left JOIN jos_jcalpro_events_seats AS s ON e.extid = s.extid
left JOIN jos_jcalpro_events_types_xref AS t ON e.extid = t.extid
In my creating routine there is no difference but the created INSERT has actually 2 lines less. How can that be?
Here is the important part of the code:
$resource = JcalproDataImport::getInstance();
// IMPORT
$pdo = $resource->getConnection('import', 'pdo');
$events = $pdo->query(
'SELECT * FROM jos_jcalpro_events AS e '
.'left JOIN jos_jcalpro_events_seats AS s ON e.extid = s.extid '
.'left JOIN jos_jcalpro_events_types_xref AS t ON e.extid = t.extid;')->fetchAll(PDO::FETCH_ASSOC);
// MAPPING
$map = array(
'picture'=>null, 'cat'=>null, 'day'=>null, 'month'=>null, 'year'=>null, 'recur_type'=>null, 'checked_out'=>null,
'checked_out_time'=>null, 'payment_type'=>null, 'id'=>null,
'extid'=>'id', 'contact'=>'contact_info', 'start_date'=>'start', 'end_date'=>'end', 'recur_val'=>'repeat_period',
'recur_end_type'=>'repeat_end_type_id', 'recur_count'=>'repeat_end_after_occurrences',
'recur_until'=>'repeat_end_after_date', 'wp_only'=>'only_in_wp', 'eticket'=>'prevent_sending_eticket',
'region'=>'region_id', 'onlinebooking'=>'online_booking_form', 'organiser'=>'organiser_name'
);
// OUTPUT
$pdo = $resource->switchConnection('export', 'pdo');
$pdo->query('SET foreign_key_checks = 0');
foreach ($events as $values)
{
$values['chargeable_status'] = (strtolower($values['payment_type']) == 'p') ? 1 : 0;
$values['repeat_period_type_id'] = $repeat_period_type_map[$values['recur_type']];
foreach($map as $from=>$to)
{
if ($to!== null){ $values[$to] = $values[$from]; }
unset( $values[$from] );
}
$values['created_at'] = '2008-01-01';
$values['updated_at'] = $values['created_at'];
if (!$event) {
foreach ($values as $field=>$value)
{
$sql .= ', '.$field;
$sqlValues .= ', "'.mysql_real_escape_string($value).'"';
}
$sql = 'INSERT INTO event ( '.substr($sql, 2).' ) VALUES '.PHP_EOL.'( '.substr($sqlValues, 2).' )';
}else{
foreach ($values as $value)
{
$sqlValues .= ', "'.mysql_real_escape_string($value).'"';
}
$sql .= ', ( '.substr($sqlValues, 2).' )';
}
$event[] = $values;
}
$pdo->query($sql)
But it works fine for other queries, so that I can’t believe in a problem there.
There is no reason
left JOINwould work differently thanLEFT JOIN. That’s very unlikely to be related to your problem.Do your column name mapping with column aliases in your
SELECT.Omit columns you don’t want by naming those columns you do want, instead of using
SELECT *.Prepare the
INSERTonce, with named parameters matching the column alias names. Then useexecute($values)to insert each row fetched from theSELECT.Here’s approximately how I’d write it: