Page 1 of 1

useing php to dump mysql!

Posted: Wed Apr 30, 2003 2:46 am
by bombshell
(mod-edit: added

Code: Select all

-tag)

Code: Select all

<?
//CODE BY Crossday
//MODIFY BY bombshell
//DATE 2003-04-28
function _create_table($table)
{  
  $tabledump = "DROP TABLE IF EXISTS $table;\n";
		$tabledump .= "CREATE TABLE $table (\n";

		$firstfield = 1;

		$result = mysql_query("SHOW FIELDS FROM $table");
		//0->Field
		//1->Type
		//2->Null
		//3->Default
		//4->Extra
		while ($field = mysql_fetch_array($result)) 
		{
			if (!$firstfield) 
			{
				$tabledump .= ",\n";
			} 
			else 
			{
				$firstfield = 0;
			}
			$tabledump .= "\t$field[0] $field[1]";
			if ($field[2] == "YES") 
			{
				$tabledump .= " ";
			}
			else
			{
				$tabledump .= " NOT NULL";
			}

			if (!empty($field["3"])) 
			{
				$tabledump .= " default '$field[3]'";
			}
			if ($field[4] != "") 
			{
				$tabledump .= " $field[4]";
			}
		}
        mysql_free_result($result);
	    $result1 = mysql_query("SHOW KEYS FROM $table");
		while ($key = mysql_fetch_array($result1)) 
		{
			$kname = $key['Key_name'];
			if ($kname != "PRIMARY" and $key['Non_unique'] == 0) 
			{
				$kname="UNIQUE|$kname";
			}
			if(!is_array($index[$kname])) 
			{
				$index[$kname] = array();
			}
			$index[$kname][] = $key['Column_name'];
		}
        mysql_free_result($result1);

		while(list($kname, $columns) = @each($index))
		{
			$tabledump .= ",\n";
			$colnames = implode($columns, ",");

			if($kname == "PRIMARY")
			{
				$tabledump .= "\tPRIMARY KEY ($colnames)";
			} 
			else 
			{
				if (substr($kname,0,6) == "UNIQUE") 
				{
					$kname = substr($kname,7);
				}

				$tabledump .= "\tKEY $kname ($colnames)";

			}
		}

		$tabledump .= "\n);\n\n";
		
		$result2 = mysql_query("SELECT * FROM $table");
		$numfields = mysql_num_fields($result2);
		$numrows = mysql_num_rows($result2);
		while ($row = mysql_fetch_row($result2)) 
		{
			$comma = "";
			$tabledump .= "INSERT INTO $table VALUES(";
			for($i = 0; $i < $numfields; $i++) 
			{
				$tabledump .= $comma."'".mysql_escape_string($row[$i])."'";
				$comma = ",";
			}
			$tabledump .= ");\n";
		}
	


	$tabledump .= "\n";
    mysql_free_result($result2);
		
  return $tabledump;
  
} 
?>

Posted: Wed Apr 30, 2003 4:20 am
by twigletmac
Do you have a question - or did you just want to show off your script?

Mac

re:

Posted: Thu May 01, 2003 2:40 am
by bombshell
Sorry! I first come here and my english is poor!
I don't agree with you on that becase someone come here look afer help and I mean this script by any possibility help somebody.Please don't use show off wording. Thanks!

Posted: Thu May 01, 2003 3:25 am
by []InTeR[]
What you try'ing to say is:

You posted your script here, with the idee that your script maybe help somebody else?

Posted: Thu May 01, 2003 7:15 am
by twigletmac
which is fine - but I thought I'd ask in case there was a question in there somewhere

By show off I just meant exhibit.

Mac

Posted: Thu May 01, 2003 7:23 am
by []InTeR[]
If your gonna 'show off' do it right. :twisted:

I think he is one of the first here, without a question. :D

Posted: Thu May 01, 2003 7:30 pm
by phice
There's nothing better then making things easier for the programmer.