useing php to dump mysql!

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
bombshell
Forum Newbie
Posts: 4
Joined: Wed Apr 30, 2003 2:46 am

useing php to dump mysql!

Post 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;
  
} 
?>
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

Do you have a question - or did you just want to show off your script?

Mac
bombshell
Forum Newbie
Posts: 4
Joined: Wed Apr 30, 2003 2:46 am

re:

Post 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!
[]InTeR[]
Forum Regular
Posts: 416
Joined: Thu Apr 24, 2003 6:51 am
Location: The Netherlands

Post by []InTeR[] »

What you try'ing to say is:

You posted your script here, with the idee that your script maybe help somebody else?
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post 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
[]InTeR[]
Forum Regular
Posts: 416
Joined: Thu Apr 24, 2003 6:51 am
Location: The Netherlands

Post by []InTeR[] »

If your gonna 'show off' do it right. :twisted:

I think he is one of the first here, without a question. :D
User avatar
phice
Moderator
Posts: 1416
Joined: Sat Apr 20, 2002 3:14 pm
Location: Dallas, TX
Contact:

Post by phice »

There's nothing better then making things easier for the programmer.
Image Image
Post Reply