PHP MySQL Database

PHP MySQL Database

In the previous tutorial, we create PHP Connection To MySQL. For this follow-up tutorial, I decided to make a tutorial on How To Create MySQL Database. For creating a database, it will consist one or more tables in one database. We create a database and name it as “myDatabase”.

Creating MySQL Database Using MySQLi and PDO

Example database name "myDatabase". Example MySQLi Object-Oriented
  1. <?php
  2. $servername = "localhost";
  3. $username = "username";
  4. $password = "password";
  5.  
  6. // Create connection
  7. $conn = new mysqli($servername, $username, $password);
  8. // Check connection
  9. if ($conn->connect_error) {
  10. die("Connection Error. Check your syntax." . $conn->connect_error);
  11. }
  12.  
  13. // Create database
  14. $sql = "CREATE DATABASE myDatabase";
  15. if ($conn->query($sql) === TRUE) {
  16. echo "Successfully Created The Database";
  17. } else {
  18. echo "Error creating database. Check the Syntax." . $conn->error;
  19. }
  20.  
  21. $conn->close();
  22. ?>
Example database name "myDatabase". Example MySQLi Procedural
  1. <?php
  2. $servername = "localhost";
  3. $username = "username";
  4. $password = "password";
  5.  
  6. // Create connection
  7. $conn = mysqli_connect($servername, $username, $password);
  8. // Check connection
  9. if (!$conn) {
  10. die("Connection Error. Check your syntax." . mysqli_connect_error());
  11. }
  12.  
  13. // Create database
  14. $sql = "CREATE DATABASE myDatabase";
  15. if (mysqli_query($conn, $sql)) {
  16. echo "Successfully Created The Database";
  17. } else {
  18. echo "Error creating database. Check the Syntax." . mysqli_error($conn);
  19. }
  20.  
  21. mysqli_close($conn);
  22. ?>
Example database name "myDatabase". Example PDO (PHP Data Objects)
  1. <?php
  2. $servername = "localhost";
  3. $username = "username";
  4. $password = "password";
  5.  
  6. try {
  7. $conn = new PDO("mysql:host=$servername;dbname=myDatabase", $username, $password);
  8. // set the PDO error mode to exception
  9. $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  10. $sql = "CREATE DATABASE myDatabase";
  11. // use exec() because no results are returned
  12. $conn->exec($sql);
  13. echo "Successfully Created The Database<br>";
  14. }
  15. catch(PDOException $e)
  16. {
  17. echo $sql . "<br>" . $e->getMessage();
  18. }
  19.  
  20. $conn = null;
  21. ?>
I use PHPMyAdmin to create a database in MySQL. After creating my database in PHPMyAdmin, this is my query using PDO in shortcodes.
  1. <?php
  2. $conn = new PDO("mysql:host=localhost;dbname=myDatabase", 'username', 'password');
  3. ?>
Share us your thoughts and comments below. Thank you so much for dropping by and reading this tutorial post. For more updates, don’t hesitate and feel free to visit this website more often and please share this with your friends or email me at [email protected]. Practice Coding. Thank you very much.

Add new comment