Articles

PHP MySQL - ALTER to drop Column

PHP MySQL - ALTER to drop Column


Let’s discuss how to drop column from an existing table in MYSQL through PHP  in the XAMPP Server.

Consider the table - Medicine present in the database - hospital snapshot with the following structure:

 

 

We can see that there are only SIX columns.

ALTER in MySQL is used to add.drop and change the datatype of the column in a table.

 

SQL ALTER DROP COLUMN - Syntax

 

ALTER TABLE TABLE_NAME DROP COLUMN name_of_the_column;

name_of_the_column refers to the column name to be removed from the table.

 

Approach-

1.Specify the credentials to create a connection object.
The credentials include XAMPP Server localhost name, username, password and database name.
$connecting_data =  new mysqli($server, $user, $pwd, $data);

2.Specify the SQL Query that includes the ALTER
$my_query = ALTER TABLE TABLE_NAME DROP COLUMN name_of_the_column;

3.Check if the column is removed or not.
if ($connecting_data->query($my_query) === TRUE) {
  echo "Column dropped Succesfully!!.";
} else {
  echo "Error: " . $connecting_data->error;
}

4. Close the Connection.
$connecting_data->close();

Example 1:

Let's drop the Number column from the Medicine table.

<?php

// Specify the server
$server = "localhost";

// Specify the user
$user = "root";

// Specify the password
$pwd = "";

// Specify the database 
$data = "hospital";

// Let's create connection by using the above details
$connecting_data =  new mysqli($server, $user, $pwd, $data);

$my_query = "ALTER TABLE Medicine DROP COLUMN Number;";


if ($connecting_data->query($my_query) === TRUE) {
  echo "Number column dropped Succesfully!!.";
} else {
  echo "Error: " . $connecting_data->error;
}

$connecting_data->close();
?>

Output:

So the column - Number is removed. let's check in xampp server.

 

 

Number column is now not available.

 

Example 2:

Let's drop the Address column from the Medicine table.

<?php

// Specify the server
$server = "localhost";

// Specify the user
$user = "root";

// Specify the password
$pwd = "";

// Specify the database 
$data = "hospital";

// Let's create connection by using the above details
$connecting_data =  new mysqli($server, $user, $pwd, $data);

$my_query = "ALTER TABLE Medicine DROP COLUMN Address;";


if ($connecting_data->query($my_query) === TRUE) {
  echo "Address column dropped Succesfully!!.";
} else {
  echo "Error: " . $connecting_data->error;
}

$connecting_data->close();
?>


Output:

So the column - Address is removed. let's check in xampp server.

 

 

Address column is now not available.

 

Conclusion

In this article we seen how to remove/drop the existing  column using ALTER command in PHP-MySQL in XAMPP Server.


PHP MySql

Would you like to see your article here on tutorialsinhand. Join Write4Us program by tutorialsinhand.com

About the Author
Gottumukkala Sravan Kumar 171FA07058
B.Tech (Hon's) - IT from Vignan's University. Published 1400+ Technical Articles on Python, R, Swift, Java, C#, LISP, PHP - MySQL and Machine Learning
Page Views :    Published Date : Jun 14,2024  
Please Share this page

Related Articles

Like every other website we use cookies. By using our site you acknowledge that you have read and understand our Cookie Policy, Privacy Policy, and our Terms of Service. Learn more Got it!