}
function echo_sql_and_die($query,$error_msg) {
//this function provides a little more info than the usual die(mysql_error())
//it first outputs the whole query, then the error_msg (usually mysql_error())
//then dies
echo "SQL ERROR OCCURRED ";
echo "ATTEMPTED QUERY: ".$query." ";
echo "RECEIVED ERROR MESSAGE: ".$error_msg." ";
}
function interpret_fieldtype($type) {
// interprets the mysql type and returns a type string
// that can be interpreted by the GetSQLValueString function
if(strncmp($type,"int",3) == 0)
return "int";
elseif(strncmp($type,"varchar",7) == 0)
return "text";
elseif(strcmp($type,"date") == 0)
return "date";
elseif(strcmp($type,"time") == 0)
return "time";
elseif(strncmp($type,"enum",4) == 0)
return "text";
elseif(strcmp($type,"float") == 0)
return "float";
elseif(strcmp($type,"double") == 0)
return "double";
else
return "text";
}
function dump_table()
{
$numargs = func_num_args();
$arglist = func_get_args();
for ($iarg = 0; $iarg < $numargs; $iarg += 2) {
switch($arglist[$iarg]) {
case 'table':
$table_name = $arglist[$iarg+1];
break;
case 'key_fld':
$key_fld = $arglist[$iarg+1];
break;
case 'key_val':
$key_val = $arglist[$iarg+1];
break;
case 'get_vals':
$get_val_fieldname_list = explode(',',$arglist[$iarg+1]);
break;
default:
echo sprintf("function dump_table: unrecognized tag '%s'",$arglist[$iarg+1]);
}
}
$sql_get_fieldnames = sprintf('describe `%s`',$table_name);
$result_get_fieldnames = mysql_query($sql_get_fieldnames) or die(mysql_error());
while($row_get_fieldnames = mysql_fetch_assoc($result_get_fieldnames)) {
$fld = $row_get_fieldnames['Field'];
$fieldnames[] = $fld;
$quoted_fieldnames[] = '`' . $fld . '`';
$types[$fld] = interpret_fieldtype($row_get_fieldnames['Type']);
}
if(isset($key_fld))
$sql_get_records = sprintf('select * from `%s` where `%s` in (%s)',$table_name,$key_fld,$key_val);
else
$sql_get_records = sprintf('select * from `%s`',$table_name);
$result_records = mysql_query($sql_get_records) or echo_sql_and_die($sql_get_records,mysql_error());
while($next_row = mysql_fetch_assoc($result_records)) {
unset($vals);
//we could just assume that the fields are going to be in the same order as listed in $fieldnames, but
//it's better to be safe about this.
foreach($fieldnames as $field) {
$new_col = addslashes($next_row[$field]);
$vals[] = GetSQLValueString($new_col,$types[$field]);
}
//$insert_vals contains the values of all the fields for a single record
$insert_vals[] = '(' . implode(',',$vals) . ')';
if(isset($get_val_fieldname_list)) {
foreach($get_val_fieldname_list as $get_val_fieldname) {
$new_val = GetSQLValueString($next_row[$get_val_fieldname],$types[$get_val_fieldname]);
if(!isset($get_val_array[$get_val_fieldname]) || !in_array($new_val,$get_val_array[$get_val_fieldname]))
$get_val_array[$get_val_fieldname][] = $new_val;
}
}
}
// using insert ignore here, so that if a duplicate key exists, it won't be overwritten, but the query will still proceed and a warning will be issued instead
if(!empty($insert_vals))
//using a tag here to indicate a carriage return. Inserting a \n in a string doesn't work. The will need to be replaced by actual linefeeds later
$sql_insert = 'insert ignore into `' . $table_name . '` (' . implode(',',$quoted_fieldnames) . ') values ' . implode(',',$insert_vals) . ';';
else
$sql_insert = '';
if(isset($get_val_fieldname_list)) {
$idx = 0;
foreach($get_val_fieldname_list as $get_val_fieldname) {
$get_val_array[$get_val_fieldname] = array_unique($get_val_array[$get_val_fieldname]);
$return_vals[$idx] = implode(',',$get_val_array[$get_val_fieldname]);
$idx++;
}
$return_array = array_merge(array($sql_insert),$return_vals);
return $return_array;
}
else
return $sql_insert;
} //end function dump_table()
//unless "Export" button was pressed, send the HTTP headers
if(!isset($_POST['action']) || ($_POST['action'] != "Submit")) {
?>
Export Attributes
Ensemble Attribute Export Utility
This utility exports one or more Ensemble attributes, and optionally, trials and stimuli associated with the attributes.
The attributes, trials, and stimuli will be exported to a file containing sql insert statements. The file can then be used
to import the records to another Ensemble database with phpMyAdmin or an equivalent utility. Note that primary keys are not
remapped using this method, so record importing is best performed on a new Ensemble installation. Usage of this utility is simple:
Select one or more attributes that you wish to export in the menu.
If you wish to export associated trials, select "Export Trials."
If you wish to export stimuli that are associated with the attribute either through
the trials, or through the stimulus_x_attribute table, select "Export Stimuli."
Optionally, enter a filestub. If omitted, a default filestub will be used.
Click Submit. The SQL file should download immediately.