PHP JT unit4
PHP JT unit4
MySQL Commands:
1. MySQL Create Database
● create database db1;
● The CREATE DATABASE statement is used to create a database
in MySQL.
● The following examples create a database named "myDB":
<?php
$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;
}
$conn->close();
?>
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";
// Create connection
$conn = new mysqli($servername, $username, $password,
$dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$conn->close();
?>
The following examples update the record with id=2 in the "MyGuests"
table:
Example (MySQLi Object-oriented)
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$conn->close();
?>
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";
// Create connection
$conn = new mysqli($servername, $username, $password,
$dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$conn->close();
?>
6) MySQL Select Query
● The SELECT statement is used to select data from one or more
tables:
● SELECT column_name(s) FROM table_name
● or we can use the * character to select ALL columns from a
table:
● SELECT * FROM table_name
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";
// Create connection
$conn = new mysqli($servername, $username, $password,
$dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "id: " . $row["id"]. " - Name: " . $row["firstname"]. " " .
$row["lastname"]. "<br>";
}
} else {
echo "0 results";
}
$conn->close();
?>
// Create connection
$conn = new mysqli($servername, $username, $password,
$dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$conn->close();
?>
8) where Query:
● The WHERE clause is used to filter records.
● The WHERE clause is used to extract only those records that
fulfill a specified condition.
● SELECT column_name(s) FROM table_name WHERE
column_name operator value
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "id: " . $row["id"]. " - Name: " . $row["firstname"]. " " .
$row["lastname"]. "<br>";
}
} else {
echo "0 results";
}
$conn->close();
?>
Database Handling Using PHP with MySQL
● MySQL is a relational database management system based on the
Structured Query Language,
● which is the popular language for accessing and managing the
records in the database.
● MySQL is open-source and free software under the GNU license.
It is supported by Oracle Company.
Database :
● A database is an application that stores the organized collection
of records.
● It can be accessed and managed by the user very easily.
● It allows us to organize data into tables, rows, columns, and
indexes to find the relevant information very quickly.
● MySQL is currently the most popular database management
system software used for managing the relational database.
● It is open-source database software,with relevant information
very quickly.
MySQL Works
● MySQL follows the working of Client-Server Architecture.
● This model is designed for the end users called clients to access
the resources from a central computer known as a server using
network services.
● Here, the clients make requests through a graphical user interface
(GUI), and the server will give the desired output as soon as the
instructions are matched.
● The process of MySQL environment is the same as the
client-server model
Functions in Mysql:
Parameter Values
Parameter Description
connection Required. Specifies the MySQL connection to use
username Required. Specifies the MySQL user name
password Required. Specifies the MySQL password
dbname Required. Specifies the database to change
Example - Object Oriented style
● Change the user of the specified database connection:
<?php
$con=mysqli_connect("localhost","my_user","my_password","my_db");
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
exit();
}
mysqli_close($con);
?>
Syntax:
Object oriented style:
$mysqli -> close()
Procedural style:
mysqli_close(connection)
Parameter Values
Parameter Description
connection: Required. Specifies the MySQL connection to
close
<?php
$mysqli = new
mysqli("localhost","my_user","my_password","my_db");
Syntax
Object oriented style:
$mysqli -> new mysqli(host, username, password, dbname, port, socket)
Procedural style:
mysqli_connect(host, username, password, dbname, port, socket)
Parameter Values
Parameter Description
host Optional. Specifies a host name or an IP address
username Optional. Specifies the MySQL username
password Optional. Specifies the MySQL password
dbname Optional. Specifies the default database to be
used
port Optional. Specifies the port number to attempt to
connect to the MySQL server
socket Optional. Specifies the socket or named pipe to
be used
<?php
$mysqli = new mysqli("localhost","my_user","my_password","my_db");
// Check connection
if ($mysqli -> connect_errno)
{
echo "Failed to connect to MySQL: " . $mysqli -> connect_error;
exit();
}
?>
Syntax:
// Check connection
if ($mysqli -> connect_errno)
{
echo "Failed to connect to MySQL: " . $mysqli -> connect_error;
exit();
}
?>
Syntax
Object oriented style:
$mysqli -> select_db(name)
Procedural style:
mysqli_select_db(connection, name)
Parameter Description
connection Required. Specifies the MySQL connection to use
name Required. Specifies the database name
Example - Object Oriented style
● Change the default database for the connection:
<?php
$mysqli = new
mysqli("localhost","my_user","my_password","my_db");
// Change db to "test" db
$mysqli -> select_db("test");
// Create connection
$conn = new mysqli($servername, $username, $password);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
?>
<?php
// Database configuration
$servername = "localhost"; // Change this to your database
$username = "root"; // Change this to your database username
$password = ""; // Change this to your database 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 IF NOT EXISTS Newdb1"; //
Change 'my_database' to your desired database name
if ($conn->query($sql) === TRUE) {
echo "Database created successfully";
} else {
echo "Error creating database: " . $conn->error;
}
// Close connection
$conn->close();
?>
<?php
// Database configuration
$servername = "localhost"; // Change this to your database server
$username = "root"; // Change this to your database username
$password = ""; // Change this to your database password
$database = "Newdb"; // Change this to your database name
// Create connection
$conn = new mysqli($servername, $username, $password, $database);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
//Selecting DB
$conn->select_db($database);
// Close connection
mysqli_close($con);
?>
<?php
// Database configuration
$servername = "localhost"; // Change this to your database server
$username = "root"; // Change this to your database username
$password = ""; // Change this to your database password
$database = "Newdb"; // Change this to your database name
// Create connection
$conn = new mysqli($servername, $username, $password,
$database); // Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// SQL to create table
$sql = "CREATE TABLE IF NOT EXISTS users (
id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
firstname VARCHAR(30) NOT NULL,
lastname VARCHAR(30) NOT NULL,
email VARCHAR(50),
reg_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON
UPDATE CURRENT_TIMESTAMP )";
// Execute SQL query
if ($conn->query($sql) === TRUE) {
echo "Table users created successfully";
} else {
echo "Error creating table: " . $conn->error;
}
// Close connection
$conn->close();
?>
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";
// Create connection
$conn = new mysqli($servername, $username, $password,
$dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "id: " . $row["id"]. " - Name: " . $row["firstname"]. " " .
$row["lastname"]. "<br>";
}
} else {
echo "0 results";
}
$conn->close();
?>
The following examples update the record with id=2 in the "MyGuests"
table:
Example (MySQLi Object-oriented)
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$conn->close();
?>