IWT unit-V
IWT unit-V
MY SQL
Databases are useful for storing information categorically. A company may have
a database with the following tables:
Employees
Products
Customers
Orders
// Creating connection
$conn = new mysqli($servername, $username, $password);
// Checking connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
?>
Output:
// Creating connection
$conn = mysqli_connect($servername, $username, $password);
// Checking connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
echo "Connected successfully";
?>
Output:
Using PDO procedure: PDO stands for PHP Data Objects. That is, in this
method we connect to the database using data objects in PHP as
described below:
Syntax:
<?php
$servername = "localhost";
$username = "username";
$password = "password";
try {
$conn = new PDO("mysql:host=$servername;dbname=myDB", $username,
$password);
// setting the PDO error mode to exception
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
echo "Connected successfully";
}
catch(PDOException $e)
{
echo "Connection failed: " . $e->getMessage();
}
?>
Output:
// 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;
}
$conn->close();
?>
Selecting a database
PHP uses mysqli_select_db function to select the database on which queries are
to be performed. This function takes two parameters and returns TRUE on
success or FALSE on failure.
Syntax:
mysqli_select_db ( mysqli $link , string $dbname ) : bool
Parameter & Description
$link
Required - A link identifier returned by mysqli_connect() or
mysqli_init().
$dbname
Required - Name of the database to be connected.
Exampl:
<html>
<head>
<title>Selecting MySQL Database</title>
</head>
<body>
<?php
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = 'root@123';
$conn = mysqli_connect($dbhost, $dbuser, $dbpass);
if(! $conn ) {
die('Could not connect: ' . mysqli_error($conn));
}
echo 'Connected successfully<br />';
$retval = mysqli_select_db( $conn, 'TUTORIALS' );
if(! $retval ) {
die('Could not select database: ' .
mysqli_error($conn));
}
echo "Database TUTORIALS selected successfully\n";
mysqli_close($conn);
?>
</body>
</html>
Listing a database
We can write the following PHP script to get the list of available MySQL
databases −
Example:
<?php
$con = mysql_connect("localhost", "userid", "password");
if (!$con) {
die('Could not connect: ' . mysql_error());
}
$db_list = mysql_list_dbs($con);
PHP MY Admin
Features of phpMyAdmin
phpMyAdmin supports several features that are given below:
o phpMyAdmin can create, alter, browse, and drop databases, views, tables,
columns, and indexes.
o It can display multiple results sets through queries and stored procedures.
o phpMyAdmin use stored procedure and queries to display multiple results sets.
o It supports foreign keys and InnoDB tables.
o phpMyAdmin can track the changes done on databases, views, and tables.
o We can also create PDF graphics of our database layout.
o phpMyAdmin can be exported into various formats such as XML, CSV, PDF,
ISO/IEC 26300 - OpenDocument Text and Spreadsheet.
o It supports mysqli, which is the improved MySQL extension.
o phpMyAdmin can interact with 80 different languages.
o phpMyAdmin can edit, execute, and bookmark any SQL-statements and even
batch-queries.
o By using a set of pre-defined functions, it can transform stored data into any
format. For example - BLOB-data as image or download-link.
o It provides the facility to backup the database into different forms.
phpMyAdmin | Advantages:
Prerequisite