Online training and video tutorials
Enter     Registration
     Contacts

 
     
 
Max Azimov
St. Petersburg

Articles [97]

User's video [259]





PHP Create a MySQL Database
 
A database consists of one or more tables.
 
You will need special CREATE privileges to create or to delete a MySQL database.
 
Create a MySQL Database Using MySQLi and PDO
 
The CREATE DATABASE statement is used to create a database in MySQL.
 
The following examples create a database named "myDB":
 
Example (MySQLi Object-oriented)
  1. <?php
  2. $servername = "localhost";
  3. $username = "username";
  4. $password = "password";
  5. // Create connection
  6. $conn = new mysqli($servername, $username, $password);
  7. // Check connection
  8. if ($conn->connect_error) {
  9.     die("Connection failed: " . $conn->connect_error);
  10. // Create database
  11. $sql = "CREATE DATABASE myDB";
  12. if ($conn->query($sql) === TRUE) {
  13.     echo "Database created successfully";
  14. } else {
  15.     echo "Error creating database: " . $conn->error;
  16. }
  17. $conn->close();
  18. ?>
 
Note: When you create a new database, you must only specify the first three arguments to the mysqli object (servername, username and password).
 
Tip: If you have to use a specific port, add an empty string for the database-name argument, like this: new mysqli("localhost", "username", "password", "", port)
 
Example (MySQLi Procedural)
  1. <?php
  2. $servername = "localhost";
  3. $username = "username";
  4. $password = "password";
  5. // Create connection
  6. $conn = mysqli_connect($servername, $username, $password);
  7. // Check connection
  8. if (!$conn) {
  9.     die("Connection failed: " . mysqli_connect_error());
  10. }
  11. // Create database
  12. $sql = "CREATE DATABASE myDB";
  13. if (mysqli_query($conn, $sql)) {
  14.     echo "Database created successfully";
  15. } else {
  16.     echo "Error creating database: " . mysqli_error($conn);
  17. }
  18. mysqli_close($conn);
  19. ?>
 
Note: The following PDO example create a database named "myDBPDO":
 
Example (PDO)
  1. <?php
  2. $servername = "localhost";
  3. $username = "username";
  4. $password = "password";
  5. try {
  6.     $conn = new PDO("mysql:host=$servername;dbname=myDB", $username, $password);
  7.     // set the PDO error mode to exception
  8.     $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  9.     $sql = "CREATE DATABASE myDBPDO";
  10.     // use exec() because no results are returned
  11.     $conn->exec($sql);
  12.     echo "Database created successfully<br>";
  13.     }
  14. catch(PDOException $e)
  15.     {
  16.     echo $sql . "<br>" . $e->getMessage();
  17.     }
  18. $conn = null;
  19. ?>
 
Tip: A great benefit of PDO is that it has exception class to handle any problems that may occur in our database queries. If an exception is thrown within the try{ } block, the script stops executing and flows directly to the first catch(){ } block. In the catch block above we echo the SQL statement and the generated error message.


2016-12-10
564 views




 
     
 
Map      Distance education, online learning courses. Project PLANETSTUDY - distance learning online.
© 2012 PlanetStudy

Catalog.299.ru

The site is a media provider
Яндекс.Метрика
 
English USA
Australia Русский