PHP_Unit5
PHP_Unit5
PHP (Hypertext Preprocessor) is a widely used server-side scripting language for web development. It is
commonly used to interact with SQL databases such as MySQL to store, retrieve, and manipulate data
dynamically.
LDAP is a protocol used to access and maintain directory information services. It is often used for
authentication and user management in enterprise applications.
<?php
$ldap_server = "ldap://example.com";
if ($ldap_conn) {
if ($bind) {
} else {
?>
PHP connectivity refers to how PHP interacts with databases like MySQL to store, retrieve, and
manipulate data dynamically. This is essential for building web applications such as login systems, online
shopping carts, content management systems, etc.
PHP provides two main ways to connect with MySQL:
Create Table
USE my_database;
);
<?php
// Create connection
// Check connection
if (!$conn) {
?>
<?php
mysqli_close($conn);
?>
PHP provides built-in functions and external libraries to send and receive emails. The mail() function is
the simplest way to send emails, while external libraries like PHPMailer offer more advanced features
such as attachments, SMTP authentication, and HTML formatting.
PHP has a built-in mail() function to send emails. However, this function only works if the server is
properly configured to send emails (like with Sendmail on Linux servers). Many shared hosting services
allow this, but it won’t work in local environments like XAMPP unless configured.
<?php
$message = "Hello, this is a test email sent from PHP!"; // Email body
// Send Email
} else {
?>
PHP cannot directly receive emails, but it can fetch emails from an email server using IMAP or POP3.
<?php
// Connect to mailbox
// Fetch emails
if ($emails) {
$subject = $header->subject;
$from = $header->fromaddress;
} else {
// Close connection
imap_close($inbox);
?>
Manipulating data means performing CRUD operations in MySQL using PHP. CRUD stands for:
After successfully connecting, we can perform Create, Read, Update, and Delete operations.
Inserting data means adding new records into a MySQL database table using PHP.
<?php
$sql = "INSERT INTO users (name, email, password) VALUES ('John Doe', 'john@example.com', '12345')";
if (mysqli_query($conn, $sql)) {
} else {
?>
INSERT INTO users (name, email, password) VALUES (...) adds new data to the table.
mysqli_query($conn, $sql); executes the query.
Retrieving data from MySQL means fetching stored records from a database table using SQL's SELECT
statement and displaying them using PHP.
<?php
if (mysqli_num_rows($result) > 0) {
echo "ID: " . $row["id"] . " - Name: " . $row["name"] . " - Email: " . $row["email"] . "<br>";
} else {
?>
SELECT id, name, email FROM users; fetches all user records.
mysqli_fetch_assoc($result); fetches rows one by one.
(C) Update Data in MySQL
The UPDATE statement in MySQL is used to modify existing records in a table. You can update one or
multiple columns based on a specific condition using the WHERE clause.
<?php
if (mysqli_query($conn, $sql)) {
} else {
?>
The DELETE statement in MySQL is used to remove specific records from a table. It is commonly used
when data is no longer needed or needs to be replaced with updated information.
<?php
if (mysqli_query($conn, $sql)) {
} else {
?>
DELETE FROM users WHERE name='John Doe'; removes a record from the table.