首頁 > 後端開發 > php教程 > PHP备份数据库为SQL文件 并可下载源码_PHP教程

PHP备份数据库为SQL文件 并可下载源码_PHP教程

WBOY
發布: 2016-07-13 17:42:25
原創
754 人瀏覽過

  这是一个将指定数据库里的所有表备份为一个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>
登入後複製
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板