Home  >  Article  >  Backend Development  >  Implementation method of php database deletion

Implementation method of php database deletion

藏色散人
藏色散人Original
2020-11-05 09:55:352336browse

php删除数据库的方法:首先创建一个PHP示例文件;然后通过mysql_connect函数连接数据库;最后通过执行语句“DROP DATABASE test_db”来删除数据库即可。

Implementation method of php database deletion

推荐:《PHP视频教程

使用 PHP 删除 MySQL 数据库

删除一个数据库

如果数据库不再需要那么它将可以被永远删除。您可以使用 SQL 命令传递给 mysql_query 执行删除一个数据库。

例子

尝试以下例子来删除一个数据库。

<?php
    $dbhost = &#39;localhost:3036&#39;;
    $dbuser = &#39;root&#39;;
    $dbpass = &#39;rootpassword&#39;;
    $conn = mysql_connect($dbhost, $dbuser, $dbpass);
    if(! $conn )
    {
      die(&#39;Could not connect: &#39; . mysql_error());
    }
    $sql = &#39;DROP DATABASE test_db&#39;;
    $retval = mysql_query( $sql, $conn );
    if(! $retval )
    {
      die(&#39;Could not delete database db_test: &#39; . mysql_error());
    }
    echo "Database deleted successfully\\n";
    mysql_close($conn);
    ?>

警告:对于删除一个数据库和表,它是非常危险的。所以删除任何表或数据库之前你应该确保你所做的一切是自愿的。

删除一个表

它通过 mysql_query 函数再次的发出一个 SQL 命令来删除任何数据库和数据表。但使用这个命令时要非常小心,因为这样做你可以在你的桌面上删除一些重要的信息。

尝试以下示例删除一个表:

<?php
    $dbhost = &#39;localhost:3036&#39;;
    $dbuser = &#39;root&#39;;
    $dbpass = &#39;rootpassword&#39;;
    $conn = mysql_connect($dbhost, $dbuser, $dbpass);
    if(! $conn )
    {
      die(&#39;Could not connect: &#39; . mysql_error());
    }
    $sql = &#39;DROP TABLE employee&#39;;
    $retval = mysql_query( $sql, $conn );
    if(! $retval )
    {
      die(&#39;Could not delete table employee: &#39; . mysql_error());
    }
    echo "Table deleted successfully\\n";
    mysql_close($conn);
    ?>

The above is the detailed content of Implementation method of php database deletion. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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