Heim >Backend-Entwicklung >PHP-Tutorial >PHP备份数据库为SQL文件 并可下载源码_PHP教程
这是一个将指定数据库里的所有表备份为一个SQL文件,可下载。这个源码来自dedecms程序,功能挺多,也很实用,但是代码的质量还有待提高
<ol class="dp-c"> <li class="alt"><span><span></span></span></li> <li><span> </span></li> <li class="alt"> <span class="comment">/****** 备份数据库结构 ******/</span><span> </span> </li> <li><span> </span></li> <li class="alt"> <span> </span><span class="comment">/* </span> </li> <li><span><span class="comment"> 函数名称:table2sql() </span> </span></li> <li class="alt"><span><span class="comment"> 函数功能:把表的结构转换成为SQL </span> </span></li> <li><span><span class="comment"> 函数参数:$table: 要进行提取的表名 </span> </span></li> <li class="alt"><span><span class="comment"> 返 回 值:返回提取后的结果,SQL集合 </span> </span></li> <li><span><span class="comment"> 函数作者:heiyeluren </span> </span></li> <li class="alt"><span><span class="comment"> */</span><span> </span></span></li> <li><span> </span></li> <li class="alt"> <span> </span><span class="keyword">function</span><span> table2sql(</span><span class="vars">$table</span><span>) </span> </li> <li><span> { </span></li> <li class="alt"> <span> </span><span class="keyword">global</span><span> </span><span class="vars">$db</span><span>; </span> </li> <li> <span> </span><span class="vars">$tabledump</span><span> = </span><span class="string">"DROP TABLE IF EXISTS $table; "</span><span>; </span> </li> <li class="alt"> <span> </span><span class="vars">$createtable</span><span> = </span><span class="vars">$db</span><span>--->query(</span><span class="string">"SHOW CREATE TABLE $table"</span><span>); </span> </li> <li> <span> </span><span class="vars">$create</span><span> = </span><span class="vars">$db</span><span>->fetch_row(</span><span class="vars">$createtable</span><span>); </span> </li> <li class="alt"> <span> </span><span class="vars">$tabledump</span><span> .= </span><span class="vars">$create</span><span>[1].</span><span class="string">"; "</span><span>; </span> </li> <li> <span> </span><span class="keyword">return</span><span> </span><span class="vars">$tabledump</span><span>; </span> </li> <li class="alt"><span> } </span></li> <li><span> </span></li> <li class="alt"><span> </span></li> <li> <span> </span><span class="comment">/****** 备份数据库结构和所有数据 ******/</span><span> </span> </li> <li class="alt"> <span> </span><span class="comment">/* </span> </li> <li><span><span class="comment"> 函数名称:data2sql() </span> </span></li> <li class="alt"><span><span class="comment"> 函数功能:把表的结构和数据转换成为SQL </span> </span></li> <li><span><span class="comment"> 函数参数:$table: 要进行提取的表名 </span> </span></li> <li class="alt"><span><span class="comment"> 返 回 值:返回提取后的结果,SQL集合 </span> </span></li> <li><span><span class="comment"> 函数作者:heiyeluren </span> </span></li> <li class="alt"><span><span class="comment"> */</span><span> </span></span></li> <li> <span> </span><span class="keyword">function</span><span> data2sql(</span><span class="vars">$table</span><span>) </span> </li> <li class="alt"><span> { </span></li> <li> <span> </span><span class="keyword">global</span><span> </span><span class="vars">$db</span><span>; </span> </li> <li class="alt"> <span> </span><span class="vars">$tabledump</span><span> = </span><span class="string">"DROP TABLE IF EXISTS $table; "</span><span>; </span> </li> <li> <span> </span><span class="vars">$createtable</span><span> = </span><span class="vars">$db</span><span>->query(</span><span class="string">"SHOW CREATE TABLE $table"</span><span>); </span> </li> <li class="alt"> <span> </span><span class="vars">$create</span><span> = </span><span class="vars">$db</span><span>->fetch_row(</span><span class="vars">$createtable</span><span>); </span> </li> <li> <span> </span><span class="vars">$tabledump</span><span> .= </span><span class="vars">$create</span><span>[1].</span><span class="string">"; "</span><span>; </span> </li> <li class="alt"><span> </span></li> <li> <span> </span><span class="vars">$rows</span><span> = </span><span class="vars">$db</span><span>->query(</span><span class="string">"SELECT * FROM $table"</span><span>); </span> </li> <li class="alt"> <span> </span><span class="vars">$numfields</span><span> = </span><span class="vars">$db</span><span>->num_fields(</span><span class="vars">$rows</span><span>); </span> </li> <li> <span> </span><span class="vars">$numrows</span><span> = </span><span class="vars">$db</span><span>->num_rows(</span><span class="vars">$rows</span><span>); </span> </li> <li class="alt"> <span> </span><span class="keyword">while</span><span> (</span><span class="vars">$row</span><span> = </span><span class="vars">$db</span><span>->fetch_row(</span><span class="vars">$rows</span><span>)) </span> </li> <li><span> { </span></li> <li class="alt"> <span> </span><span class="vars">$comma</span><span> = </span><span class="string">""</span><span>; </span> </li> <li> <span> </span><span class="vars">$tabledump</span><span> .= </span><span class="string">"INSERT INTO $table VALUES("</span><span>; </span> </li> <li class=" </p> <p align=" left> <div style="display:none;"> <span id="url" itemprop="url">http://www.bkjia.com/PHPjc/486044.html</span><span id="indexUrl" itemprop="indexUrl">www.bkjia.com</span><span id="isOriginal" itemprop="isOriginal">true</span><span id="isBasedOnUrl" itemprop="isBasedOnUrl">http://www.bkjia.com/PHPjc/486044.html</span><span id="genre" itemprop="genre">TechArticle</span><span id="description" itemprop="description">这是一个将指定数据库里的所有表备份为一个SQL文件,可下载。这个源码来自dedecms程序,功能挺多,也很实用,但是代码的质量还有待提高...</span> </div> <div class="art_confoot"></div> </li> </ol>