How can a beginner with little to no experience in PHP create a MySQL database?
To create a MySQL database using PHP, a beginner can use the mysqli extension to connect to the MySQL server and execute SQL queries to create a new database. The mysqli_connect() function is used to establish a connection to the MySQL server, and then the mysqli_query() function can be used to execute SQL queries to create the database.
<?php
$servername = "localhost";
$username = "root";
$password = "";
$conn = mysqli_connect($servername, $username, $password);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
// Create database
$sql = "CREATE DATABASE myDB";
if (mysqli_query($conn, $sql)) {
echo "Database created successfully";
} else {
echo "Error creating database: " . mysqli_error($conn);
}
mysqli_close($conn);
?>
Keywords
Related Questions
- What potential pitfalls should a PHP beginner be aware of when working on a project involving network printer management?
- What is the purpose of passing an upload to a function in PHP?
- What resources or tools are recommended for beginners to learn PHP and implement scripts like the one for Gmail backup?