MySQL Create DB

Creating a database in MySQL using PHP involves establishing a connection and executing the appropriate SQL query. Below is a step-by-step guide:


Create a Database

Example - MySQLi Object-oriented
connect_error) { die("Connection failed: " . $conn->connect_error); } // SQL query to create a database $dbname = "my_new_database"; $sql = "CREATE DATABASE $dbname"; // Execute the query if ($conn->query($sql) === TRUE) { echo "Database '$dbname' created successfully"; } else { echo "Error creating database: " . $conn->error; } // Close the connection $conn->close(); ?>
Example - MySQLi Object-oriented

Note

The CREATE DATABASE SQL command is used to create the new database.

You can also create a database using the PDO extension:

Example - PDO
setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); // SQL query to create a database $dbname = "my_new_database"; $sql = "CREATE DATABASE $dbname"; // Execute the query $conn->exec($sql); echo "Database '$dbname' created successfully"; } catch (PDOException $e) { echo "Error creating database: " . $e->getMessage(); } ?>

Whereisstuff is simple learing platform for beginer to advance level to improve there skills in technologies.we will provide all material free of cost.you can write a code in runkit workspace and we provide some extrac features also, you agree to have read and accepted our terms of use, cookie and privacy policy.
© Copyright 2024 www.whereisstuff.com. All rights reserved. Developed by whereisstuff Tech.