Home > php教程 > php手册 > PHP备份数据库为SQL文件 并可下载源码

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

WBOY
Release: 2016-06-13 10:41:11
Original
1365 people have browsed it

  这是一个将指定数据库里的所有表备份为一个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>
">
</ol>
Copy after login
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template