My code below takes an xml file and inserts it into a database table. However, I would also like to save the full insert query as a backup. Is it possible to write out the query built by wpdb so that I can save it to a file?
function lc_course_database_convert_to_sql( $file ) {
global $wpdb;
//$wpdb->show_errors();
$xml = simplexml_load_file( $file['url'] );
$tablename = $wpdb->prefix . 'courses';
$mydata = new SimpleXmlIterator( $file['url'], 0, true );
for ( $mydata->rewind();$mydata->valid();$mydata->next() ) {
//create an array with the column names and corresponding values
$insertarray = array();
foreach ( $mydata->current() as $k=>$v ) {
$insertarray[$k]= (string)$v;
}
$wpdb->insert(
$tablename,
$insertarray,
array(
'%d',
'%s',
'%d',
'%s',
'%s',
'%s',
'%s',
'%s',
'%s',
'%s',
'%s',
'%s',
'%s',
'%s',
'%d',
'%d',
'%s',
'%s',
'%s',
'%s',
'%s',
'%s',
'%s',
'%s',
'%s',
'%d',
'%d',
'%s',
'%s',
'%s',
'%s',
'%s',
'%s',
'%s',
'%s',
'%s',
'%s',
'%s',
'%s',
'%d',
'%s',
'%s',
'%d',
'%s',
'%s',
'%d',
'%d',
'%s',
'%s',
'%d',
'%d',
'%d',
'%d',
'%d',
'%d',
'%d',
'%d',
'%d',
'%d',
'%d',
'%d',
'%d',
'%d',
)
);
}
}