PHP MySQL Database
Submitted by alpha_luna on Wednesday, May 11, 2016 - 12:48.
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- <?php
- $servername = "localhost";
- $username = "username";
- $password = "password";
- // Create connection
- $conn = new mysqli($servername, $username, $password);
- // Check connection
- if ($conn->connect_error) {
- }
- // Create database
- $sql = "CREATE DATABASE myDatabase";
- if ($conn->query($sql) === TRUE) {
- echo "Successfully Created The Database";
- } else {
- echo "Error creating database. Check the Syntax." . $conn->error;
- }
- $conn->close();
- ?>
- <?php
- $servername = "localhost";
- $username = "username";
- $password = "password";
- // Create connection
- // Check connection
- if (!$conn) {
- }
- // Create database
- $sql = "CREATE DATABASE myDatabase";
- echo "Successfully Created The Database";
- } else {
- }
- ?>
- <?php
- $servername = "localhost";
- $username = "username";
- $password = "password";
- try {
- $conn = new PDO("mysql:host=$servername;dbname=myDatabase", $username, $password);
- // set the PDO error mode to exception
- $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
- $sql = "CREATE DATABASE myDatabase";
- // use exec() because no results are returned
- echo "Successfully Created The Database<br>";
- }
- catch(PDOException $e)
- {
- echo $sql . "<br>" . $e->getMessage();
- }
- $conn = null;
- ?>
- <?php
- $conn = new PDO("mysql:host=localhost;dbname=myDatabase", 'username', 'password');
- ?>
Add new comment
- 437 views