A database consists of one or more tables. But before we can create tables, we need to create the database itself!
To create a database, we use the SQL statement CREATE DATABASE. We can execute this statement directly from our PHP script.
To run an SQL query in PHP, we use the mysqli_query() function.
<?php $servername = "localhost"; $username = "root"; $password = "your_password";// 1. Create connection $conn = mysqli_connect($servername, $username, $password);
// 2. Check connection if (!$conn) { die("Connection failed: " . mysqli_connect_error()); }
// 3. Create database $sql = "CREATE DATABASE myDB"; if (mysqli_query($conn, $sql)) { echo "Database created successfully"; } else { echo "Error creating database: " . mysqli_error($conn); }
// 4. Close the connection mysqli_close($conn); ?>
Once the database is created, you can log into your MySQL control panel (like phpMyAdmin) and see your brand new myDB database waiting for you!
Which function is used to execute an SQL query in procedural PHP?