PHP - How To Create MySQLi Database

Submitted by: 
Language: 
Visitors have accessed this post 395 times.

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="<a href="https://sourcecodester.com">Sourcecodester</a>
  11. " rel="nofollow">https://sourcecodester.com">Sourcecodester</a>
  12. </a> </div>
  13. </nav>
  14. <div class="col-md-3"></div>
  15. <div class="col-md-6 well">
  16. <h3 class="text-primary">PHP - How To Create MySQLi Database</h3>
  17. <hr style="border-top:1px dotted #ccc;"/>
  18. <div class="col-md-3"></div>
  19. <div class="col-md-6">
  20. <form method="POST" action="">
  21. <div class="form-group">
  22. <label>Database Name:</label>
  23. <input type="text" name="db_name" class="form-control" required="required"/>
  24. </div>
  25. <center><?php include 'create_db.php'?></center>
  26. <center><button name="create" class="btn btn-primary"><span class="glyphicon glyphicon-save"></span> Create Database</button></center>
  27. <form>
  28. </div>
  29. </div>
  30. </body>
  31. </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

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • You may insert videos with [video:URL]
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd> <table> <tr> <td> <th> <img> <h1> <h2> <h3> <iframe> [video]
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <asp>, <c>, <cpp>, <csharp>, <css>, <html4strict>, <java>, <javascript>, <mysql>, <php>, <python>, <sql>, <vb>, <vbnet>. The supported tag styles are: <foo>, [foo].
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.