//
// This function returns the "CREATE TABLE" syntax for mysql dbms...
//
function get_table_def_mysql($table, $crlf)
{
global $drop, $db; $schema_create = "";
$field_query = "SHOW FIELDS FROM $table";
$key_query = "SHOW KEYS FROM $table"; //
// If the user has selected to drop existing tables when doing a restore.
// Then we add the statement to drop the tables....
//
if ($drop == 1)
{
$schema_create .= "DROP TABLE IF EXISTS $table;$crlf";
} $schema_create .= "CREATE TABLE $table($crlf"; //
// Ok lets grab the fields...
//
$result = $db->sql_query($field_query);
if(!$result)
{
message_die(GENERAL_ERROR, "Failed in get_table_def (show fields)", "", __LINE__, __FILE__, $field_query);
} while ($row = $db->sql_fetchrow($result))
{
$schema_create .= ' ' . $row['Field'] . ' ' . $row['Type']; if(!empty($row['Default']))
{
$schema_create .= ' DEFAULT \'' . $row['Default'] . '\'';
} if($row['Null'] != "YES")
{
$schema_create .= ' NOT NULL';
} if($row['Extra'] != "")
{
$schema_create .= ' ' . $row['Extra'];
} $schema_create .= ",$crlf";
}
//
// Drop the last ',$crlf' off ;)
//
$schema_create = ereg_replace(',' . $crlf . '$', "", $schema_create); //
// Get any Indexed fields from the database...
//
$result = $db->sql_query($key_query);
if(!$result)
{
message_die(GENERAL_ERROR, "FAILED IN get_table_def (show keys)", "", __LINE__, __FILE__, $key_query);
} while($row = $db->sql_fetchrow($result))
{
$kname = $row['Key_name']; if(($kname != 'PRIMARY') && ($row['Non_unique'] == 0))
{
$kname = "UNIQUE|$kname";
} if(!is_array($index[$kname]))
{
$index[$kname] = array();
} $index[$kname][] = $row['Column_name'];
} while(list($x, $columns) = @each($index))
{
$schema_create .= ", $crlf"; if($x == 'PRIMARY')
{
$schema_create .= ' PRIMARY KEY (' . implode($columns, ', ') . ')';
}
elseif (substr($x,0,6) == 'UNIQUE')
{
$schema_create .= ' UNIQUE ' . substr($x,7) . ' (' . implode($columns, ', ') . ')';
}
else
{
$schema_create .= " KEY $x (" . implode($columns, ', ') . ')';
}
} $schema_create .= "$crlf);"; if(get_magic_quotes_runtime())
{
return(stripslashes($schema_create));
}
else
{
return($schema_create);
}} // End get_table_def_mysql
// This function returns the "CREATE TABLE" syntax for mysql dbms...
//
function get_table_def_mysql($table, $crlf)
{
global $drop, $db; $schema_create = "";
$field_query = "SHOW FIELDS FROM $table";
$key_query = "SHOW KEYS FROM $table"; //
// If the user has selected to drop existing tables when doing a restore.
// Then we add the statement to drop the tables....
//
if ($drop == 1)
{
$schema_create .= "DROP TABLE IF EXISTS $table;$crlf";
} $schema_create .= "CREATE TABLE $table($crlf"; //
// Ok lets grab the fields...
//
$result = $db->sql_query($field_query);
if(!$result)
{
message_die(GENERAL_ERROR, "Failed in get_table_def (show fields)", "", __LINE__, __FILE__, $field_query);
} while ($row = $db->sql_fetchrow($result))
{
$schema_create .= ' ' . $row['Field'] . ' ' . $row['Type']; if(!empty($row['Default']))
{
$schema_create .= ' DEFAULT \'' . $row['Default'] . '\'';
} if($row['Null'] != "YES")
{
$schema_create .= ' NOT NULL';
} if($row['Extra'] != "")
{
$schema_create .= ' ' . $row['Extra'];
} $schema_create .= ",$crlf";
}
//
// Drop the last ',$crlf' off ;)
//
$schema_create = ereg_replace(',' . $crlf . '$', "", $schema_create); //
// Get any Indexed fields from the database...
//
$result = $db->sql_query($key_query);
if(!$result)
{
message_die(GENERAL_ERROR, "FAILED IN get_table_def (show keys)", "", __LINE__, __FILE__, $key_query);
} while($row = $db->sql_fetchrow($result))
{
$kname = $row['Key_name']; if(($kname != 'PRIMARY') && ($row['Non_unique'] == 0))
{
$kname = "UNIQUE|$kname";
} if(!is_array($index[$kname]))
{
$index[$kname] = array();
} $index[$kname][] = $row['Column_name'];
} while(list($x, $columns) = @each($index))
{
$schema_create .= ", $crlf"; if($x == 'PRIMARY')
{
$schema_create .= ' PRIMARY KEY (' . implode($columns, ', ') . ')';
}
elseif (substr($x,0,6) == 'UNIQUE')
{
$schema_create .= ' UNIQUE ' . substr($x,7) . ' (' . implode($columns, ', ') . ')';
}
else
{
$schema_create .= " KEY $x (" . implode($columns, ', ') . ')';
}
} $schema_create .= "$crlf);"; if(get_magic_quotes_runtime())
{
return(stripslashes($schema_create));
}
else
{
return($schema_create);
}} // End get_table_def_mysql
解决方案 »
- 请问一下这个功能curl能实现吧?
- 为什么我做的PHP网站用IE和火狐浏览都不一样?
- 求教,关于随机生成的问题
- 网页内嵌的media player如何播放非http,rstp,mms协议的流媒体??急
- 用php与nusoap 写了一个web service。 但,用JAVA怎么调用这个web service阿?
- 分组排序,需要数量最多的结果,请达人指点!
- ***这个程序怎么改成速度变快呢?**
- 面试不爽散分ing,今天下午面试过程。
- php.ini配置问题
- 最简单的问题help me
- 我已经加载了我加载了php_iconv.dll模块,但是却Call to undefined function: iconv()
- 丢人大了,phpmyadmin2.6.0rc3连接不上mysql!
// This function is for getting the data from a mysql table.
//function get_table_content_mysql($table, $handler)
{
global $db; // Grab the data from the table.
if (!($result = $db->sql_query("SELECT * FROM $table")))
{
message_die(GENERAL_ERROR, "Failed in get_table_content (select *)", "", __LINE__, __FILE__, "SELECT * FROM $table");
} // Loop through the resulting rows and build the sql statement.
if ($row = $db->sql_fetchrow($result))
{
$handler("\n#\n# Table Data for $table\n#\n");
$field_names = array(); // Grab the list of field names.
$num_fields = $db->sql_numfields($result);
$table_list = '(';
for ($j = 0; $j < $num_fields; $j++)
{
$field_names[$j] = $db->sql_fieldname($j, $result);
$table_list .= (($j > 0) ? ', ' : '') . $field_names[$j];
}
$table_list .= ')'; do
{
// Start building the SQL statement.
$schema_insert = "INSERT INTO $table $table_list VALUES("; // Loop through the rows and fill in data for each column
for ($j = 0; $j < $num_fields; $j++)
{
$schema_insert .= ($j > 0) ? ', ' : ''; if(!isset($row[$field_names[$j]]))
{
//
// If there is no data for the column set it to null.
// There was a problem here with an extra space causing the
// sql file not to reimport if the last column was null in
// any table. Should be fixed now :) JLH
//
$schema_insert .= 'NULL';
}
elseif ($row[$field_names[$j]] != '')
{
$schema_insert .= '\'' . addslashes($row[$field_names[$j]]) . '\'';
}
else
{
$schema_insert .= '\'\'';
}
} $schema_insert .= ');'; // Go ahead and send the insert statement to the handler function.
$handler(trim($schema_insert)); }
while ($row = $db->sql_fetchrow($result));
} return(true);
}
{
global $tempfile; //fwrite($tempfile, $content . "\n");
//$backup_sql .= $content . "\n";
echo $content ."\n";
return;
}
if($backup_type != 'data')
{
echo "#\n# TABLE: " . $table_name . "\n#\n";
echo get_table_def_mysql( $table_name, "\n") . "\n";
}//数据备份
if($backup_type != 'structure')
{
$table_content_function($table_name, "output_table_content");
}
{
$DumpStr = "DROP TABLE IF EXISTS $table;\n";
$rslt = $DBObj->Query("SHOW CREATE TABLE $table");
$qrStr= $DBObj->fetch_Array($rslt);
$DumpStr .= $qrStr['Create Table'] . ";\n\n";
$rslt = $DBObj->query("SELECT * FROM $table");
$numfields = $DBObj->Num_Fields($rslt);
$startTime = time();
while($row = $DBObj->fetch_Array($rslt, MYSQL_NUM))
{
$comma = "";
$DumpStr .= "INSERT INTO $table VALUES("; for($i = 0, $cnt = $DBObj->Num_Fields($rslt); $i < $cnt; $i++)
{
$DumpStr .= $comma . "'" . mysql_escape_string($row[$i]) . "'";
$comma = " ,";
}
$DumpStr .= ");\n";
if((time() - $startTime) % 30) Header("Noop-Header: noop");
} return $DumpStr;
}
//函数名:get_table_code
//功能:返回指定数据库中表的结构,用于创建指定表的SQL语句
//参数:$link 是数据库连接
// $database 数据库名
// $table_name表名
//*****************************************************************
function get_table_code($link,$database,$table_name)
{
mysql_select_db($database, $link);
$result = mysql_query("select * from ".$table_name, $link);
$return_str = "DROP TABLE IF EXISTS ".$table_name.";\n";
$return_str .= "CREATE TABLE `".$table_name."` (\n\t";
$fields_num = (int)mysql_num_fields($result);
$fun_my['string'] = "varchar";
$fun_my['datetime'] = "datetime";
$fun_my['blob'] = "text";
$fun_my['real'] = "float";
echo "<br>";
for($i=0; $i < $fields_num; $i++)
{
$return_str .= "`".mysql_field_name($result, $i)."` ";
$type = mysql_field_type($result, $i);
if(isset($fun_my[$type]))
{ //解析数据类型
if($fun_my[$type]=="datetime" || $fun_my[$type]=="text")
{
$return_str .= $fun_my[$type]." ";
}else{
$return_str .= $fun_my[$type]."(".mysql_field_len($result, $i).") ";
}//end if
}else{
$return_str .= $type."(".mysql_field_len($result, $i).") ";
}//end if
if($i==0)
{ //
$return_str .= "NOT NULL auto_increment ";
}else{
$return_str .= mysql_field_flags($result, $i)=="not_null"?"NOT NULL ":"default NULL ";
}//end if
$return_str .= ",\n\t";
}//end for // get keys list
$result = mysql_query("SHOW KEYS FROM $table_name", $link);
while ($array=mysql_fetch_array($result)){
$kname=$array['Key_name'];
if ($kname != "PRIMARY" and $array['Non_unique'] == 0) {
$kname="UNIQUE|$kname";
}
if(!is_array($index[$kname])) {
$index[$kname] = array();
}
$index[$kname][] = $array['Column_name'];
//print_r($index[$kname]);
}
// get each key info
while(list($kname, $columns) = @each($index)){
$colnames=implode($columns,",");
if ($kname == "PRIMARY") {
// do primary key
$return_str .= "PRIMARY KEY ($colnames)\n";
} else {
// do standard key
if (substr($kname,0,6) == "UNIQUE") {
// key is unique
$kname=substr($kname,7);
}
$return_str .= "KEY $kname ($colnames)\n";
}
}$return_str .= ")TYPE=MyISAM;\n\n";
return $return_str;
}//end function//*****************************************************************
//函数名:get_table_data
//功能:返回指定数据库指定表的所有数据的,插入SQL语句
//参数:$link 是数据库连接
// $database 数据库名
// $table_name表名
//*****************************************************************
function get_table_data($link,$database,$table_name)
{
mysql_select_db($database, $link);
$result = mysql_query("select * from ".$table_name, $link);
$fields_num = (int)mysql_num_fields($result);
while($row = mysql_fetch_row($result))
{
$row_data = "INSERT INTO `".$table_name."` VALUES ('";
for($i=0;$i<$fields_num;$i++)
{
if(!isset($row[$i])) $row[$i]="NULL";
$row_data_arr[$i]=addslashes($row[$i]);
}//end for
//print_r($row_data_arr);
$row_data .= implode("','",$row_data_arr);
$row_data .= "');\n";
$table_data .= $row_data;
}//end while
$table_data .= "\n\n";
return $table_data;
}//end function