Creating a table in a database. PHP OOP example open

Creating a table in a database. PHP OOP example

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

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);

Full OOP example:

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

//Check connection errors
if($conn->connect_error){
    die("Connection error: " . $conn->connect_error);
}

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

//Check errors
if($conn->query($sql)){
    echo "Table crated";
} else{
    echo "Error: " . $conn->error;
}
$conn->close();


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