How to Add a New Field in a Table With PHP for MySQL
The MySQL "alter" statement lets you add a new field to a MySQL table, and you can run this statement from a PHP Web page. You run these PHP statements to dynamically edit your MySQL databases, which store user and site information for display on your website. The statement adds the field and loads all rows with a null value.
Instructions
-
-
1
Right-click the PHP page that you want to use to add a table field. Click "Open With," then click your PHP editor or a plain text editor, if you do not have a third party PHP editor installed.
-
2
Create the query string to add the table field. The following code creates a query string to add the "city" field to the "customers" table:
$sql = "alter table customers ADD COLUMN city VARCHAR(100)";
In this example, a column is created that can contain 100 characters. Replace "100" with the maximum amount of characters you want to allow in the column.
-
-
3
Create the database connection. The following code connects to the "business" database on the local MySQL server:
mysql_connect("localhost", "dbuser", "pass");
mysql_select_db("business")Replace "dbuser" and "pass" with your own MySQL database username and password. Replace "localhost" with the name of the MySQL database server name.
-
4
Send the query to the server and add the field to the table. The following code adds the table column:
$success = mysql_query($sql);
-
1