How can we delete an existing MySQL table by using PHP script?

MySQLMySQLi DatabasePHP

<p style="">As we know that PHP provides us the function named mysql_query to delete an existing MySQL table.</p><h2>Example</h2><p style="">To illustrate this we are deleting a table named &lsquo;Tutorials_tbl&rsquo; with the help of PHP script in the following example &minus;</p><pre class="prettyprint notranslate">&lt;html&gt; &nbsp; &nbsp;&lt;head&gt; &nbsp; &nbsp; &nbsp; &lt;title&gt;Creating MySQL Tables&lt;/title&gt; &nbsp; &nbsp;&lt;/head&gt; &nbsp; &nbsp;&lt;body&gt; &nbsp; &nbsp; &nbsp; &lt;?php &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;$dbhost = &#39;localhost:3036&#39;; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;$dbuser = &#39;root&#39;; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;$dbpass = &#39;rootpassword&#39;; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;$conn = mysql_connect($dbhost, $dbuser, $dbpass); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if(! $conn ) { &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; die(&#39;Could not connect: &#39; . mysql_error()); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;} &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;echo &#39;Connected successfully&lt;br /&gt;&#39;; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;$sql = &quot;DROP TABLE tutorials_tbl&quot;; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;mysql_select_db( &#39;TUTORIALS&#39; ); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;$retval = mysql_query( $sql, $conn ); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if(! $retval ) { &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;die(&#39;Could not delete table: &#39; . mysql_error()); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;} &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;echo &quot;Table deleted successfully &quot;; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;mysql_close($conn); &nbsp; &nbsp; &nbsp; ?&gt; &nbsp; &nbsp;&lt;/body&gt; &lt;/html&gt;</pre>
raja
Updated on 22-Jun-2020 13:43:01

Advertisements