Database program in php

PHP | MySQL ( Creating Database )

What is a database?
Database is a collection of inter-related data which helps in efficient retrieval, insertion and deletion of data from database and organizes the data in the form of tables, views, schemas, reports etc. For Example, university database organizes the data about students, faculty, and admin staff etc. which helps in efficient retrieval, insertion and deletion of data from it.

We know that in MySQL to create a database we need to execute a query. You may refer to this article for the SQL query to create data-bases.

The basic steps to create MySQL database using PHP are:

  • Establish a connection to MySQL server from your PHP script as described in this article.
  • If the connection is successful, write a SQL query to create a database and store it in a string variable.
  • Execute the query.

We have already learnt about establish a connection and creating variables in PHP. We can execute the query from our PHP script in 3 different ways as described below:

  1. Using MySQLi Object-oriented procedure: If the MySQL connection is established using Object-oriented procedure then we can use the query() function of mysqli class to execute our query as described in the below syntax. Syntax:
// Creating a connection $conn = new mysqli($servername, $username, $password); // Check connection if ($conn->connect_error) < die("Connection failed: " . $conn->connect_error); > // Creating a database named newDB $sql = "CREATE DATABASE newDB"; if ($conn->query($sql) === TRUE) < echo "Database created successfully with the name newDB"; >else < echo "Error creating database: " . $conn->error; > // closing connection $conn->close(); ?>

Note:Specify the three arguments servername, username and password to the mysqli object whenever creating a database. Output:

Читайте также:  What is buffer overflow in java

Using MySQLi Procedural procedure: If the MySQL connection is established using procedural procedure then we can use the mysqli_query() function of PHP to execute our query as described in the below syntax. Syntax:

// Creating connection $conn = mysqli_connect($servername, $username, $password); // Checking connection if (!$conn) < die("Connection failed: " . mysqli_connect_error()); >// Creating a database named newDB $sql = "CREATE DATABASE newDB"; if (mysqli_query($conn, $sql)) < echo "Database created successfully with the name newDB"; >else < echo "Error creating database: " . mysqli_error($conn); >// closing connection mysqli_close($conn); ?>

Output:

Using PDO procedure: If the MySQL connection is established using PDO procedure then we can execute our query as described in the below syntax. Syntax:

// setting the PDO error mode to exception $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $sql = "CREATE DATABASE newDB"; // using exec() because no results are returned $conn->exec($sql); echo "Database created successfully with the name newDB"; > catch(PDOException $e) < echo $sql . "
" . $e->getMessage(); > $conn = null; ?>

Note:The exception class in PDO is used 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. Output:

Источник

PHP MySQL Database

With PHP, you can connect to and manipulate databases.

MySQL is the most popular database system used with PHP.

What is MySQL?

  • MySQL is a database system used on the web
  • MySQL is a database system that runs on a server
  • MySQL is ideal for both small and large applications
  • MySQL is very fast, reliable, and easy to use
  • MySQL uses standard SQL
  • MySQL compiles on a number of platforms
  • MySQL is free to download and use
  • MySQL is developed, distributed, and supported by Oracle Corporation
  • MySQL is named after co-founder Monty Widenius’s daughter: My
Читайте также:  Html for spacing between words

The data in a MySQL database are stored in tables. A table is a collection of related data, and it consists of columns and rows.

Databases are useful for storing information categorically. A company may have a database with the following tables:

PHP + MySQL Database System

  • PHP combined with MySQL are cross-platform (you can develop in Windows and serve on a Unix platform)

Database Queries

A query is a question or a request.

We can query a database for specific information and have a recordset returned.

Look at the following query (using standard SQL):

The query above selects all the data in the «LastName» column from the «Employees» table.

To learn more about SQL, please visit our SQL tutorial.

Download MySQL Database

If you don’t have a PHP server with a MySQL Database, you can download it for free here: http://www.mysql.com

Facts About MySQL Database

MySQL is the de-facto standard database system for web sites with HUGE volumes of both data and end-users (like Facebook, Twitter, and Wikipedia).

Another great thing about MySQL is that it can be scaled down to support embedded database applications.

Источник

PHP Create a MySQL Database

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)

$servername = «localhost»;
$username = «username»;
$password = «password»;

// Create connection
$conn = new mysqli($servername, $username, $password);
// Check connection
if ($conn->connect_error) die(«Connection failed: » . $conn->connect_error);
>

// Create database
$sql = «CREATE DATABASE myDB»;
if ($conn->query($sql) === TRUE) echo «Database created successfully»;
> else echo «Error creating database: » . $conn->error;
>

Читайте также:  Php change server name

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)

$servername = «localhost»;
$username = «username»;
$password = «password»;

// Create connection
$conn = mysqli_connect($servername, $username, $password);
// Check connection
if (!$conn) die(«Connection failed: » . mysqli_connect_error());
>

// Create database
$sql = «CREATE DATABASE myDB»;
if (mysqli_query($conn, $sql)) echo «Database created successfully»;
> else echo «Error creating database: » . mysqli_error($conn);
>

Note: The following PDO example create a database named «myDBPDO»:

Example (PDO)

$servername = «localhost»;
$username = «username»;
$password = «password»;

try $conn = new PDO(«mysql:host=$servername», $username, $password);
// set the PDO error mode to exception
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql = «CREATE DATABASE myDBPDO»;
// use exec() because no results are returned
$conn->exec($sql);
echo «Database created successfully
«;
> catch(PDOException $e) echo $sql . «
» . $e->getMessage();
>

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.

Источник

Оцените статью