>  기사  >  백엔드 개발  >  深入探讨PHP删除数据库表的具体方式_PHP教程

深入探讨PHP删除数据库表的具体方式_PHP教程

WBOY
WBOY원래의
2016-07-15 13:30:06935검색

在我们对网站进行更新维护时,可能对删除相同数据库表的操作比较头疼,今天我们就向大家具体讲解PHP删除数据库表具体实现代码如下

<ol class="dp-xml">
<li class="alt"><span><span>function deldata($dbname,$tableflag){  </span></span></li>
<li>
<span>$</span><span class="attribute">db_host</span><span> = </span><span class="attribute-value">'localhost'</span><span>;  </span>
</li>
<li class="alt">
<span>$</span><span class="attribute">db_port</span><span> = </span><span class="attribute-value">'3306'</span><span>;  </span>
</li>
<li>
<span>$</span><span class="attribute">db_user</span><span> = </span><span class="attribute-value">'user'</span><span>;  </span>
</li>
<li class="alt">
<span>$</span><span class="attribute">db_pass</span><span> = </span><span class="attribute-value">'password'</span><span>;  </span>
</li>
<li>
<span>$</span><span class="attribute">connect</span><span> =</span><span class="attribute-value">mysql_connect</span><span>($db_host,$db_user,$db_pass);  </span>
</li>
<li class="alt"><span>mysql_select_db($dbname);  </span></li>
<li>
<span>$</span><span class="attribute">result</span><span> = </span><span class="attribute-value">mysql_query</span><span>("show table status from $dbname",$connect);  </span>
</li>
<li class="alt">
<span>$</span><span class="attribute">data</span><span>=</span><span class="attribute-value">mysql_fetch_array</span><span>($result);  </span>
</li>
<li>
<span>while($</span><span class="attribute">data</span><span>=</span><span class="attribute-value">mysql_fetch_array</span><span>($result)) {  </span>
</li>
<li class="alt">
<span>$</span><span class="attribute">table</span><span>=</span><span class="attribute-value">mysubstr</span><span>($data[Name],"_");  </span>
</li>
<li>
<span>if($</span><span class="attribute">table</span><span>==$tableflag){  </span>
</li>
<li class="alt"><span>//测试之用  </span></li>
<li><span>/*echo $data[Name];  </span></li>
<li class="alt"><span>        echo "  </span></li>
<li><span>";  </span></li>
<li class="alt"><span>        echo $table;  </span></li>
<li><span>        echo "  </span></li>
<li class="alt"><span>";*/  </span></li>
<li><span>mysql_query("drop table $data[Name]");  </span></li>
<li class="alt"><span>}  </span></li>
<li><span>}  </span></li>
<li class="alt"><span>return true;  </span></li>
<li><span>}  </span></li>
<li class="alt"><span>/*截取某个特定字符前的所有字符函数  </span></li>
<li><span>*$str 为待截取字符串  </span></li>
<li class="alt"><span>*$flag 特定字符如“_”  </span></li>
<li><span>*/  </span></li>
<li class="alt"><span>function mysubstr($str,$flag){  </span></li>
<li>
<span>$</span><span class="attribute">pos</span><span>=</span><span class="attribute-value">strpos</span><span>($str,$flag);  </span>
</li>
<li class="alt"><span>return substr($str,0,$pos);  </span></li>
<li><span>}  </span></li>
<li class="alt">
<span class="tag">?></span><span> </span>
</li>
</ol>

<ol class="dp-xml">
<li class="alt"><span><span>$</span><span class="attribute">dbname</span><span>=</span><span class="attribute-value">"shujukuming"</span><span>;//数据库名  </span></span></li>
<li>
<span>$</span><span class="attribute">tableflag</span><span>=</span><span class="attribute-value">"xx"</span><span>;//要删除的表的前缀,有这个前缀的表才删除,标志出是哪一个用户  </span>
</li>
<li class="alt"><span>deldata($dbname,$tableflag);  </span></li>
<li>
<span>//$</span><span class="attribute">test</span><span>=</span><span class="attribute-value">mysubstr</span><span>("cdb_account_log","_");  </span>
</li>
<li class="alt"><span>//echo $test;  </span></li>
<li>
<span class="tag">?></span><span> </span>
</li>
</ol>

PHP删除数据库表更改之处在:

1.开头处

<ol class="dp-xml">
<li class="alt"><span><span>function deldata($dbname,$tableflag){  </span></span></li>
<li>
<span>$</span><span class="attribute">db_host</span><span> = </span><span class="attribute-value">'localhost'</span><span>;  </span>
</li>
<li class="alt">
<span>$</span><span class="attribute">db_port</span><span> = </span><span class="attribute-value">'3306'</span><span>;  </span>
</li>
<li>
<span>$</span><span class="attribute">db_user</span><span> = </span><span class="attribute-value">'user'</span><span>;  </span>
</li>
<li class="alt">
<span>$</span><span class="attribute">db_pass</span><span> = </span><span class="attribute-value">'password'</span><span>;  </span>
</li>
</ol>

改为自己的数据库地址,账号和密码即可

2.结尾处

<ol class="dp-xml">
<li class="alt"><span><span>$</span><span class="attribute">dbname</span><span>=</span><span class="attribute-value">"shujukuming"</span><span>;//数据库名  </span></span></li>
<li>
<span>$</span><span class="attribute">tableflag</span><span>=</span><span class="attribute-value">"xx"</span><span>;//要删除的表的前缀,有这个前缀的表才删除,标志出是哪一个用户  </span>
</li>
<li class="alt"><span>deldata($dbname,$tableflag);  </span></li>
<li>
<span>//$</span><span class="attribute">test</span><span>=</span><span class="attribute-value">mysubstr</span><span>("cdb_account_log","_");  </span>
</li>
<li class="alt"><span>//echo $test;  </span></li>
<li>
<span class="tag">?></span><span> </span>
</li>
<li class="alt"><span> </span></li>
</ol>

改为自己的数据库名和想删掉的表前缀就可完成PHP删除数据库表。
可以复制上面的代码保存为.php,再上传到空间目录打开
推荐可以下载此.php文件http://flysh.toypark.in//up/1255275586.umd下载后改后缀为php,名字自取再上传打开即可。


www.bkjia.comtruehttp://www.bkjia.com/PHPjc/446309.htmlTechArticle在我们对网站进行更新维护时,可能对删除相同数据库表的操作比较头疼,今天我们就向大家具体讲解 PHP删除数据库表具体实现代码如下...
성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.