PHP MySQL Create Table

PHP Tutorial

🗄️ PHP MySQL Create Table Tutorial

After creating a database, PHP MySQL Create Table to store your data. PHP can execute SQL CREATE TABLE statements using mysqli or PDO.


1️⃣ Using mysqli (Procedural)


 

Explanation:

  • id → Primary key, auto-incremented

  • name → VARCHAR, cannot be NULL

  • email → UNIQUE constraint

  • age → Integer

  • reg_date → Timestamp, default is current time


2️⃣ Using mysqli (Object-Oriented)


 


3️⃣ Using PDO


 

PDO::exec() is used for queries that don’t return results like CREATE, UPDATE, DELETE.


4️⃣ Key Points

  1. Data types: INT, VARCHAR, TEXT, DATE, TIMESTAMP, etc.

  2. Constraints:

    • PRIMARY KEY → unique identifier

    • UNIQUE → prevents duplicate values

    • NOT NULL → column cannot be empty

    • AUTO_INCREMENT → automatically increments numeric field

  3. Always check connection and query errors.

  4. Tables must be created inside an existing database.

You may also like...