UNIT 5 PHP NOTES
UNIT 5 PHP NOTES
Sr.no Syllabus
5.1 Introduction to MySQL, Create a Database.
5.2 Connecting to a MySQL database: MySQL database server from PHP.
PYQs Repeated.
Syntax in php
<?php
$conn = mysqli_connect("localhost", "username", "password", "database_name");
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
?>
Features of MySQL in PHP.
MySQL is an open-source relational database used to store and manage data efficiently.
It supports SQL (Structured Query Language) for creating, reading, updating, and deleting
records.
MySQL can be easily connected with PHP using built-in functions like mysqli or PDO.
It offers high speed, reliability, and scalability for both small and large web applications.
MySQL supports multiple users and provides secure data access with user roles and privileges.
It works well with PHP to build dynamic websites that interact with databases in real-.
Creating a DataBase:
Creating a database in PHP is done using MySQLi or PDO functions.
The mysqli_connect() function is used to connect to the MySQL server.
The CREATE DATABASE SQL statement is used to create a new database.
You must check if the connection is successful before executing the query.
Database creation is mostly done during setup or installation of applications.
Steps for creating database .
Step 1: Set Server Details
First, define variables to store your database connection details like servername,
username, and password.
Syntax:
<?php
$conn = new mysqli("localhost", "root", "");
$sql = "CREATE DATABASE dbname";
$conn->query($sql); Ouput
$conn->close();
Database created successfully
?>
Example:
<?php
$servername = "localhost";
$username = "root";
$password = "";
Steps:
Syntax:
mysqli_connect("hostname", "username", "password", "database_name");
Solution1:
<?php Output
$servername = "localhost";
Connected successfully
$username = "root";
$password = "";
// Connection
$conn = new mysqli($servername,$username, $password);
// For checking if connection issuccessful or not
if ($conn->connect_error)
{
die("Connection failed: ". $conn->connect_error);
}
echo "Connected successfully";
?>
.
Definition of `mysqli_connect()` in PHP
### **Syntax:**
```php
mysqli_connect(host, username, password, dbname);
```
### **Example:**
Output
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
echo "Connected successfully";
?>
Database operations in PHP involve connecting to a database, performing queries, and handling
the results.
Using PHP, you can perform common operations like insert, select, update, and delete data from
a database.
PHP commonly works with MySQL or MySQLi to manage database interactions.
These operations allow dynamic websites to store and retrieve user data efficiently.
Proper use of database operations improves functionality, performance, and security of web
applications.
Example:
<?php
require_once 'login.php'; // contains DB connection details
$conn = new mysqli($hn, $un, $pw, $db);
if ($conn->connect_error) die($conn->connect_error);
$query = "INSERT INTO student (rollno, name, percent) VALUES ('CO103', 'Reena Patel',
98.45)";
$result = $conn->query($query); Output
if (!$result) die("Database access failed: " . $conn->error);
Data inserted successfully!
else
echo "Data inserted successfully!";
?>
Retrieving the Query Result.
Retrieving data in PHP from a database is done using the SQL SELECT statement.
This statement is used to fetch records from one or more tables in a database.
PHP uses the query() function to execute the SQL SELECT query on the database
connection.
The result returned by query() is stored in a result object which contains the records.
num_rows is used to count how many rows (records) were fetched from the table.
data_seek() and fetch_array() methods help loop through each record from the result.
Retrieved data is usually displayed using HTML elements like <table> for a user-friendly
output.
Syntax:
$result = $conn->query("SELECT * FROM table_name");
Example:
<?php
require_once 'login.php'; // Database connection file
Syntax:
UPDATE table_name
SET column1 = value1, column2 = value2
WHERE condition;
Example:
Output
<?php
$servername = "localhost"; Record updated successfully
$username = "root";
$password = "";
$dbname = "clg";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// Update query
$sql = "UPDATE staff SET lastname='technology' WHERE id=1";
$conn->close();
?>
Delete operations on table data:
- The `DELETE` statement in SQL is used to remove one or more records from a table.
- It is usually combined with the `WHERE` clause to delete specific records based on a
condition.
- Without a `WHERE` clause, all rows in the table will be deleted, so it should be used carefully.
- The SQL `DELETE` query is created and passed to PHP's `query()` function to execute it.
- PHP connects to the database using `mysqli` or `PDO` before running the `DELETE` query.
- Example: Deleting a record from the `staff` table where `id = 1`.
- After successful deletion, a message like "Record deleted successfully" is shown.
Syntax:
DELETE FROM table_name WHERE condition;
**Example:
```php
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "clg";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// SQL DELETE statement
$sql = "DELETE FROM staff WHERE id=1";
// Execute the query
Output
if ($conn->query($sql) === TRUE) {
echo "Record deleted successfully";
} else {
Record deleted successfully
echo "Error deleting record: " . $conn->error;
}
// Close the connection
$conn->close();
?>