Creating a table in MySQL database. PHP function example open

Creating a table in MySQL database. PHP function example

Approved. Code works!
This is exactly the working code that is verified by the moderator or site administrators
Tested: PHP 5.6 +, mySQL 5.7

MySQL uses the CREATE TABLE command to create a table.
For example, create a table users, It will have three columns: id, name and age:

CREATE TABLE Users (id INTEGER AUTO_INCREMENT PRIMARY KEY, name VARCHAR(30), age INTEGER);

Function example:

//Connect to DB
$conn = mysqli_connect("localhost", "root", "password", "testdb");

//Check connection errors
if (!$conn) {
  die("Connection error: " . mysqli_connect_error());
}

//Creating table
$sql = "CREATE TABLE Users (id INTEGER AUTO_INCREMENT PRIMARY KEY, name VARCHAR(30), age INTEGER);";

//Check errors
if(mysqli_query($conn, $sql)){
    echo "Table created";
} else{
    echo "Error: " . mysqli_error($conn);
}

mysqli_close($conn);

0

More

Leave a Reply

Your email address will not be published. Required fields are marked *

How many?: 22 + 22

lil-code© | 2022 - 2024
Go Top
Authorization
*
*
Registration
*
*
*
*
Password generation