سلام
میخواستم بدونم اگه بخوام تو php با استفاده از کدها از یه جدول خاص export بگیرم باید چه کدی رو بنویسم؟
به صورتی که میخوام وقتی روی یه لینک کلیک شد export گرفته شه!
ممنون:40:
Printable View
سلام
میخواستم بدونم اگه بخوام تو php با استفاده از کدها از یه جدول خاص export بگیرم باید چه کدی رو بنویسم؟
به صورتی که میخوام وقتی روی یه لینک کلیک شد export گرفته شه!
ممنون:40:
این کد هارو نگاه کن تو این کد هم از دیتابیس به صورت xml می شه بک آپ گرفت هم sql خودم نوشتم ببین یه چیزایی یاد می گیری
[PHP]<? include '../config.php';
if(isset($_GET["del"])){
$a = @unlink($_GET['del']);
}
if(isset($_GET['d'])){
if(strip_tags($_GET['d'])=='xml'){
//get all the tables
$query = 'SHOW TABLES FROM '.$dbname;
$result = mysql_query($query,$connection) or die('cannot show tables');
if(mysql_num_rows($result))
{
//prep output
$tab = "\t";
$br = "\n";
$xml = '<?xml version="1.0" encoding="UTF-8"?>'.$br;
$xml.= '<database name="'.$dbname.'">'.$br;
//for every table...
while($table = mysql_fetch_row($result))
{
//prep table out
$xml.= $tab.'<table name="'.$table[0].'">'.$br;
//get the rows
$query3 = 'SELECT * FROM '.$table[0];
$records = mysql_query($query3,$connection) or die('cannot select from table: '.$table[0]);
//table attributes
$attributes = array('name','blob','maxlength','multiple_key','no t_null','numeric','primary_key','table','type','de fault','unique_key','unsigned','zerofill');
$xml.= $tab.$tab.'<columns>'.$br;
$x = 0;
while($x < mysql_num_fields($records))
{
$meta = mysql_fetch_field($records,$x);
$xml.= $tab.$tab.$tab.'<column ';
foreach($attributes as $attribute)
{
$xml.= $attribute.'="'.$meta->$attribute.'" ';
}
$xml.= '/>'.$br;
$x++;
}
$xml.= $tab.$tab.'</columns>'.$br;
//stick the records
$xml.= $tab.$tab.'<records>'.$br;
while($record = mysql_fetch_assoc($records))
{
$xml.= $tab.$tab.$tab.'<record>'.$br;
foreach($record as $key=>$value)
{
$xml.= $tab.$tab.$tab.$tab.'<'.$key.'>'.htmlspecialchars( stripslashes($value)).'</'.$key.'>'.$br;
}
$xml.= $tab.$tab.$tab.'</record>'.$br;
}
$xml.= $tab.$tab.'</records>'.$br;
$xml.= $tab.'</table>'.$br;
}
$xml.= '</database>';
//save file
$handle = fopen($dbname.'-backup-MPC-'.md5($dbname).crc32($dbname).sha1($dbname).'.xml' ,'w+');
fwrite($handle,$xml);
fclose($handle);
}
$fname = $dbname.'-backup-MPC-'.md5($dbname).crc32($dbname).sha1($dbname).'.xml' ;
$zipname = $dbname.'-backup-MPC-'.md5($dbname).crc32($dbname).sha1($dbname).'.zip' ;
$z = fopen($zipname,'w+');
fclose($z);
$zip = new ZipArchive;
if ($zip->open($zipname) === TRUE) {
$zip->addFile($fname, $fname);
$zip->close();
@unlink($fname);
$zipname = ------_encode($zipname);
} else {
@unlink($zipname);
header('location: ?file='.$fname);
}
header('location: ?file='.$zipname);
}
elseif(strip_tags($_GET['d'])=='sql'){
$tables = '*';
//get all of the tables
if($tables == '*')
{
$tables = array();
$result = mysql_query('SHOW TABLES');
while($row = mysql_fetch_row($result))
{
$tables[] = $row[0];
}
}
else
{
$tables = is_array($tables) ? $tables : explode(',',$tables);
}
//cycle through
foreach($tables as $table)
{
$result = mysql_query('SELECT * FROM '.$table);
$num_fields = mysql_num_fields($result);
$return.= 'DROP TABLE '.$table.';';
$row2 = mysql_fetch_row(mysql_query('SHOW CREATE TABLE '.$table));
$return.= "\n\n".$row2[1].";\n\n";
for ($i = 0; $i < $num_fields; $i++)
{
while($row = mysql_fetch_row($result))
{
$return.= 'INSERT INTO '.$table.' VALUES(';
for($j=0; $j<$num_fields; $j++)
{
$row[$j] = addslashes($row[$j]);
$row[$j] = ereg_replace("\n","\\n",$row[$j]);
if (isset($row[$j])) { $return.= '"'.$row[$j].'"' ; } else { $return.= '""'; }
if ($j<($num_fields-1)) { $return.= ','; }
}
$return.= ");\n";
}
}
$return.="\n\n\n";
}
//save file
$handle = fopen('db-backup-MPC-'.(md5(implode(',',$tables))).'.sql','w+');
fwrite($handle,$return);
fclose($handle);
$fname = 'db-backup-MPC-'.(md5(implode(',',$tables))).'.sql';
$zipname = 'db-backup-MPC-'.(md5(implode(',',$tables))).'.zip';
$z = fopen($zipname,'w+');
fclose($z);
$zip = new ZipArchive;
if ($zip->open($zipname) === TRUE) {
$zip->addFile($fname, $fname);
$zip->close();
@unlink($fname);
$zipname = ------_encode($zipname);
} else {
@unlink($zipname);
header('location: ?file='.$fname);
}
header('location: ?file='.$zipname);
}
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>تهیه نسخه پشتیبان از اطلاعات سایت</title>
<link href="css/style.css" rel="stylesheet" type="text/css">
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script type="text/javascript" src="js/custom.js"></script>
</head>
<body>
<div align="center">
<div align="center" class="shadow" style="background:#DFDFDF">
<span style="font-family:tahoma; font-size:11px; color:#380; font-weight:bold; cursor:pointer" onclick="location.href='backup.php'">بخش تهیه نسخه پشتیبان از اطلاعات سایت</span>
<br />
<hr />
<br />
<div align="right" style="width:600px"><span style="font-family:tahoma; font-size:11px; color:#3399FF; font-weight:bold">انتخاب نوع نسخه پشتیبان</span></div>
<br /><br />
<? if($a){echo '<span style="font-family:tahoma; font-size:12px; color:#390" dir="rtl">حذف فایل با موفقیت انجام شد</span><br />';}?>
<div align="right" style="width:600px"><span style="font-family:tahoma; font-size:12px; color:#000000; line-height:35px;" dir="rtl">
<img src="images/Database.png" alt="database" width="32" height="32" align="absmiddle" /> <strong style="font-size:11px;">دیتابیس</strong>
<br />
  ;<a href="?d=xml" class="mpcl"><img src="images/xml.png" alt="xml" width="25" height="25" align="absmiddle" /> دریافت اطلاعات دیتابیس با پسوند XML</a>
<br />
  ;<a href="?d=sql" class="mpcl"><img src="images/sql.png" alt="sql" width="25" height="25" align="absmiddle" /> دریافت اطلاعات دیتابیس با پسوند SQL</a>
</span></div>
<? if(isset($_GET['file'])){?>
<br /><strong style="font-size:11px; font-family:tahoma;"><a href="<? echo ------_decode($_GET['file']);?>" class="mpcl">« دریافت فایل »</a></strong>
<br /><br />
<div align="right" style="width:600px"><span dir="rtl" style="font-family:tahoma; font-size:12px"><strong style="font-family:tahoma; font-size:11px; color:#390">* توجه : </strong>پس از این که دانلود فایل به اتمام رسید روی حذف فایل که در پایین قرار داده شده است کلیلک کنید تا فایل پشتیبان از روی هاست پاک گردد.<br />اینکار برای بالا بردن ضریب امنیتی سایت ضروری می باشد.</span></div>
<br /><strong style="font-size:11px; font-family:tahoma;"><a href="?del=<? echo ------_decode($_GET['file']);?>" class="mpcl">« حذف فایل »</a></strong>
<? } ?>
<br />
<hr />
<br />
</div>
</div>
</body>
</html>
[/PHP]
اینم فایل config.php که باید این طوری باشه تا مقادیر فارسی هم به درستی export کنه
[PHP]
<?PHP
$dbhost = "localhost";
$dbpass = "";
$dbusername = "root";
$dbname = "dbname";
$connection = mysql_connect($dbhost, $dbusername, $dbpass)or die(mysql_error());
mysql_select_db($dbname);
mysql_query("SET NAMES 'utf8'", $connection);
mysql_query("SET CHARACTER SET 'utf8'", $connection);
mysql_query("SET character_set_connection = 'utf8'", $connection);
?>
[/PHP]