PHP and MySQ Creating database

December 19, 2023 · Team EasyExamNotes · 1 min read (est.) · 0 comments

Share on WhatsApp

1. Connect to MySQL server

Use the mysqli_connect function with server details, including hostname, username, password, and database name (the database to connect to, not the one to create).

$conn = mysqli_connect("localhost", "your_username", "your_password", "existing_database_name");

2. Define SQL statement

Write the CREATE DATABASE statement with the desired name for new database.

$sql = "CREATE DATABASE new_database_name;";

3. Execute the query

Use the mysqli_query function to execute the SQL statement on connection.

mysqli_query($conn, $sql)

4. Close the connection

Always remember to close the connection to your MySQL server with mysqli_close($conn).

mysqli_close($conn)

Discussion

Ask a question, share your thoughts or report an error.

Sign in to participate in the discussion.

Join the discussion