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
- How can PHP developers ensure that sensitive file paths and URLs are not exposed to users in a web application?
- In what situations should $_SERVER["REMOTE_ADDR"] be preferred over $REMOTE_ADDR in PHP scripts?
- Are there any common pitfalls to avoid when implementing a survey functionality on a PHP website?