[".$db_list_row[0]."]"; } return "Databases found:".$db_list; } else if($action=="getsql"){ // user wants to generate SQL. if(!@mysql_select_db($db)) { // no such DB or user has no access to that DB return mysql_error(); } // ok, let us begin... $generated_sql = "# db2SQL v".DB2SQL_VERSION." - www.puremango.co.uk\n"; $generated_sql .= "#---------------------------------------------\n"; $generated_sql .= "# Dump of Host: [".$host."]\n"; $generated_sql .= "# DB: [".$db."]\n"; $generated_sql .= "# Generated: ".date("F jS, Y, g:i:s a")."\n"; $generated_sql .= "#---------------------------------------------\n"; $tables = (!empty($tables)) ? $tables : Array(); if(empty($tables)) { // no tables sent - get all $sql = "SHOW TABLES"; $tab_res = mysql_query($sql,$cn); while($row = @mysql_fetch_row($tab_res)) { $tables[] = $row[0]; } } else { // user specified tables to get - use them. $tables = explode(",",$tables); // if the tables they specify don't exist, then that's their problem. } if(sizeof($tables)==0) { // braindead users again. return "No tables in database"; } // do what the user wants, for each table in specified database foreach($tables as $table_name) { // blank out some values $keys = Array(); $last_key_name = ""; // get structure? if($get_structure) { $generated_sql .= "\n#\n# Structure of table [".$table_name."]\n#\n"; $generated_sql .= "CREATE TABLE IF NOT EXISTS ".$table_name." (\n"; // get fields $tab_struct_sql = "SHOW FIELDS FROM ".$table_name; $tab_struct_res = @mysql_query($tab_struct_sql,$cn); $num_fields = @mysql_num_rows($tab_struct_res); // get keys $tab_key_sql = "SHOW KEYS FROM ".$table_name; $tab_key_res = @mysql_query($tab_key_sql,$cn); $num_keys = @mysql_num_rows($tab_key_res); // add fields to SQL for($i=0 ; ($tab_struct_row = @mysql_fetch_assoc($tab_struct_res)) ; $i++) { // work out 'optional extras' $default_str = (!is_null($tab_struct_row['Default'])) ? " DEFAULT '".$tab_struct_row['Default']."'" : ''; $null_str = ($tab_struct_row['Null']!="YES") ? " NOT NULL" : ''; $extra_str = (!empty($tab_struct_row['Extra'])) ? ' '.$tab_struct_row['Extra'] : ''; // add to SQL $generated_sql .= " ".$tab_struct_row['Field']." ".$tab_struct_row['Type'].$null_str.$default_str.$extra_str; // if this is the last field AND there are no keys, don't add a comma if($i+1<$num_fields || $num_keys>0) { // otherwise add comma $generated_sql .= " ,"; } $generated_sql .= "\n"; } // add keys to SQL // this is a pain (so many ways of doing it (multiple fields -argh!)) // hence this part is a bit hacky for($i=0 ; ($tab_key_row = @mysql_fetch_assoc($tab_key_res)) ; $i++) { // if we're on a new (unprocessed) key if($last_key_name!=$tab_key_row['Key_name']) { if(!empty($last_key_name)) { // there was a key before this one // so finish it's SQL before starting this one's // chop final comma $generated_sql = substr($generated_sql,0,strlen($generated_sql)-1); // new line, etc $generated_sql .= "),\n"; } // what sort of key is it? if($tab_key_row['Key_name']=="PRIMARY") { $key_prefix = " PRIMARY KEY"; } else if($tab_key_row['Non_unique']!="1") { $key_prefix = " UNIQUE KEY ".$tab_key_row['Key_name']; } else { $key_prefix = " KEY ".$tab_key_row['Key_name']; } // add to SQL $generated_sql .= $key_prefix." ("; } // add fields to key $generated_sql .= $tab_key_row['Column_name'].","; // save key name $last_key_name = $tab_key_row['Key_name']; // if the next key name is the same, we continue adding fields to it // otherwise, we start a new line for the next key } // and then finish up keys if(!empty($last_key_name)) { // chop final comma $generated_sql = substr($generated_sql,0,strlen($generated_sql)-1); // terminate key $generated_sql .= ")\n"; } // end table structure SQL $generated_sql .= ");\n"; } // get data? if($get_data) { $generated_sql .= "\n#\n# Data for [".$table_name."]\n#\n"; // grab all data $data_sql = "SELECT * FROM ".$table_name; $data_res = @mysql_query($data_sql,$cn); if(mysql_num_rows($data_res)==0) { // nice and easy $generated_sql .= "# (no data in [".$table_name."])\n"; } while($data_row = @mysql_fetch_array($data_res,MYSQL_NUM)) { $generated_sql .= "INSERT INTO ".$table_name." VALUES("; for($i=0 ; $i0) { $dump_result = dump_data($_POST['action'],$_POST['host'],$_POST['port'],$_POST['user'],$_POST['pass'],$_POST['db'],$_POST['tables'],$_POST['structure'],$_POST['data']); if($dump_result===true) { // all groovy. exit(); } // otherwise, $dump_result is a string telling us what's gone horribly wrong. } ?> db2SQL v<?=DB2SQL_VERSION?> - www.puremango.co.uk
db2SQL v - www.puremango.co.uk

Message: ".$dump_result."

"; } ?>
host:
user
pass
action:
test connection>
list databases>
generate SQL>, if so, complete rest of form:
→get structure?>
→get data?>
db
tables
↑ ↑ - comma separated or leave blank for all - ↑ ↑