PHP - How To Create MySQLi Database

In this tutorial we will create a How To Create MySQLi Database using PHP. PHP is a server-side scripting language designed primarily for web development. It is a lean and consistent way to access databases. This means developers can write portable code much easier. It is mostly used by a newly coders for its user friendly environment. So Let's do the coding...

Getting started:

First you have to download & install XAMPP or any local server that run PHP scripts. Here's the link for XAMPP server https://www.apachefriends.org/index.html. Lastly, this is the link for the bootstrap that i used for the layout design https://getbootstrap.com/.

Creating The Interface

This is where we will create a simple form for our application. To create the forms simply copy and write it into your text editor, then save it as index.php.
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1"/>
  5. <link rel="stylesheet" type="text/css" href="css/bootstrap.css" />
  6. </head>
  7. <body>
  8. <nav class="navbar navbar-default">
  9. <div class="container-fluid">
  10. <a class="navbar-brand" href="https://sourcecodester.com">Sourcecodester</a>
  11. </div>
  12. </nav>
  13. <div class="col-md-3"></div>
  14. <div class="col-md-6 well">
  15. <h3 class="text-primary">PHP - How To Create MySQLi Database</h3>
  16. <hr style="border-top:1px dotted #ccc;"/>
  17. <div class="col-md-3"></div>
  18. <div class="col-md-6">
  19. <form method="POST" action="">
  20. <div class="form-group">
  21. <label>Database Name:</label>
  22. <input type="text" name="db_name" class="form-control" required="required"/>
  23. </div>
  24. <center><?php include 'create_db.php'?></center>
  25. <center><button name="create" class="btn btn-primary"><span class="glyphicon glyphicon-save"></span> Create Database</button></center>
  26. <form>
  27. </div>
  28. </div>
  29. </body>
  30. </html>

Creating the Main Function

This is the main function of the application. This code will generate database when the user provide the database name on the text field. To do that copy and write inside the text editor, then save it as create_db.php.
  1. <?php
  2. if(ISSET($_POST['create'])){
  3. $db_server = "localhost";
  4. $db_user = "root";
  5. $db_pass = "";
  6. $db_name = $_POST['db_name'];
  7.  
  8. $conn = mysqli_connect($db_server, $db_user, $db_pass);
  9.  
  10. if(!$conn){
  11. die("Error: Failed to connect to database".mysqli_connect_error());
  12. }
  13.  
  14. if(mysqli_query($conn, "CREATE DATABASE $db_name")){
  15. echo "<label class='text-success'>Database '".$db_name."' created successfully</label>";
  16. }else{
  17. echo "<label class='text-danger'>Error!: " . mysqli_error($conn)."</label>";
  18. }
  19.  
  20. mysqli_close($conn);
  21. }
  22.  
  23. ?>
There you have it we successfully created How To Create MySQLi Database using PHP. I hope that this simple tutorial help you to what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!

Add new comment