PHP create MySQL Tables:

A MySQL database table has its unique name, and it consists of rows and columns.

Create a MySQL table using MySQLi:

For creating a table, we use CREATE TABLE command in the MySQL database.

We will create a table with a name of “students”, which consists of five columns, “id”, “first_name”, “last_name”, “age”, and “batch”.

The columns can hold the data which can specify the data types.

 Attributes for each column:

  • NOT NULL: Every row contains a value for the column. A row cannot be null.
  • DEFAULT: We can set any default value when no value set.
  • AUTOINCREMENT: MySQL automatically increases the value of the field by one each time a new record added.
  • PRIMARY KEY: Used to identify the rows in a table uniquely.

The table should have a primary key column (like “id”). The value of the primary key column must be unique.

Download link
Download link

PHP Insert data to MySQL:

Once the database and table created, we can start inserting data into them.

  • The query must be quoted in PHP Language.
  • The string values inside the query must be quoted.
  • The numeric and word value must not be quoted.
  • A column AUTO_INCREMENT (“id”) is no need to specify in the SQL query. The MySQL will automatically add the value.
Download link
mysql delete example
PHP & MySQL Code:
// Connect to MySQL
// Delete Bobby from the "example" MySQL table

mysql_query("DELETE FROM example WHERE age='15'")
or die(mysql_error()); 
// Connect to MySQL
// Get Sandy's record from the "example" table
$result = mysql_query("UPDATE example SET age='22' WHERE age='21'")
or die(mysql_error()); 
$result = mysql_query("SELECT * FROM example WHERE age='22'")
or die(mysql_error()); 
// get the first (and hopefully only) entry from the result
$row = mysql_fetch_array( $result );
echo $row['name']." - ".$row['age']. "<br />";
Sandy Smith - 22PART4