Examples of Real-world Applications

Examples of Real-world Applications in MySQLi
MySQLi, or MySQL Improved, is a powerful PHP extension designed for interacting with MySQL databases. It provides enhanced functionality over the deprecated MySQL extension, including support for prepared statements, transactions, and both procedural and object-oriented programming.

This makes MySQLi an excellent choice for developing robust, secure, and scalable web applications. In this article, we will explore several real-world applications that leverage MySQLi, highlighting case studies and examples to demonstrate its practical usage.

E-Commerce Platforms

Case Study: An Online Store

Overview: An e-commerce platform requires a robust database system to manage products, users, orders, and payments. MySQLi’s features are ideal for handling the complex interactions and high traffic of an online store.

Implementation:

1. Database Connection: The first step is to establish a secure connection to the MySQL database using MySQLi.

				
					<?php
$mysqli = new mysqli('localhost', 'db_user', 'db_password', 'ecommerce_db');

// Check connection
if ($mysqli->connect_error) {
    die('Connection failed: ' . $mysqli->connect_error);
}
?>

				
			

2. Fetching Products: To display products, you need to retrieve data from the database.

				
					<?php
$query = "SELECT id, name, price FROM products WHERE stock > 0";
$result = $mysqli->query($query);

if ($result->num_rows > 0) {
    while ($row = $result->fetch_assoc()) {
        echo "Product: " . $row['name'] . " - Price: $" . $row['price'] . "<br>";
    }
} else {
    echo "No products available.";
}
?>

				
			

3. Handling Orders: To handle orders, use prepared statements to prevent SQL injection and manage transactions.

				
					<?php
$mysqli->begin_transaction();

try {
    $stmt = $mysqli->prepare("INSERT INTO orders (user_id, product_id, quantity, total_price) VALUES (?, ?, ?, ?)");
    $stmt->bind_param('iiii', $user_id, $product_id, $quantity, $total_price);

    // Example values
    $user_id = 1;
    $product_id = 10;
    $quantity = 2;
    $total_price = 50;

    $stmt->execute();
    $stmt->close();
    $mysqli->commit();
    echo "Order placed successfully!";
} catch (Exception $e) {
    $mysqli->rollback();
    echo "Order failed: " . $e->getMessage();
}
?>

				
			

Content Management Systems (CMS)

Case Study: A Blogging Platform

Overview: A CMS for blogging needs to manage posts, categories, comments, and user profiles. MySQLi’s object-oriented interface helps in organizing and managing these components effectively.

Implementation:

1. Connecting to the Database:

				
					<?php
$mysqli = new mysqli('localhost', 'db_user', 'db_password', 'blog_db');

// Check connection
if ($mysqli->connect_error) {
    die('Connection failed: ' . $mysqli->connect_error);
}
?>

				
			

2. Displaying Blog Posts:

				
					<?php
$query = "SELECT id, title, content, date_posted FROM posts ORDER BY date_posted DESC";
$result = $mysqli->query($query);

while ($row = $result->fetch_assoc()) {
    echo "<h2>" . $row['title'] . "</h2>";
    echo "<p>" . $row['content'] . "</p>";
    echo "<small>Posted on: " . $row['date_posted'] . "</small><br><br>";
}
?>

				
			

3. Adding New Posts:

Use prepared statements to securely insert new blog posts.

				
					<?php
$stmt = $mysqli->prepare("INSERT INTO posts (title, content, date_posted) VALUES (?, ?, ?)");
$stmt->bind_param('sss', $title, $content, $date_posted);

$title = "New Blog Post";
$content = "This is the content of the new blog post.";
$date_posted = date('Y-m-d H:i:s');

$stmt->execute();
$stmt->close();
echo "New post added successfully!";
?>

				
			

Social Networking Sites

Case Study: A Social Media Application

Overview: A social networking site requires handling user profiles, friend connections, posts, and messaging. MySQLi’s support for complex queries and transactions is beneficial in this context.

Implementation:

1. Connecting to the Database:

				
					<?php
$mysqli = new mysqli('localhost', 'db_user', 'db_password', 'social_network_db');

// Check connection
if ($mysqli->connect_error) {
    die('Connection failed: ' . $mysqli->connect_error);
}
?>

				
			

2. Fetching User Friends List:

				
					<?php
$user_id = 1; // Example user ID
$query = "SELECT friends.id, friends.name FROM friends JOIN user_friends ON friends.id = user_friends.friend_id WHERE user_friends.user_id = ?";
$stmt = $mysqli->prepare($query);
$stmt->bind_param('i', $user_id);
$stmt->execute();
$result = $stmt->get_result();

while ($row = $result->fetch_assoc()) {
    echo "Friend: " . $row['name'] . "<br>";
}
$stmt->close();
?>

				
			

3. Posting a Message:

				
					<?php
$mysqli->begin_transaction();

try {
    $stmt = $mysqli->prepare("INSERT INTO posts (user_id, message, timestamp) VALUES (?, ?, ?)");
    $stmt->bind_param('iss', $user_id, $message, $timestamp);

    $user_id = 1; // Example user ID
    $message = "Hello, world!";
    $timestamp = date('Y-m-d H:i:s');

    $stmt->execute();
    $stmt->close();
    $mysqli->commit();
    echo "Message posted successfully!";
} catch (Exception $e) {
    $mysqli->rollback();
    echo "Failed to post message: " . $e->getMessage();
}
?>

				
			

Online Booking Systems

Case Study: A Hotel Reservation System

Overview: A hotel reservation system needs to manage bookings, availability, and customer details. MySQLi’s features support handling these requirements efficiently.

Implementation:

1. Connecting to the Database:

				
					<?php
$mysqli = new mysqli('localhost', 'db_user', 'db_password', 'hotel_db');

// Check connection
if ($mysqli->connect_error) {
    die('Connection failed: ' . $mysqli->connect_error);
}
?>

				
			

2. Checking Room Availability:

				
					<?php
$check_in = '2024-12-01';
$check_out = '2024-12-10';
$query = "SELECT room_number FROM rooms WHERE room_number NOT IN (SELECT room_number FROM bookings WHERE (check_in <= ? AND check_out >= ?))";
$stmt = $mysqli->prepare($query);
$stmt->bind_param('ss', $check_out, $check_in);
$stmt->execute();
$result = $stmt->get_result();

while ($row = $result->fetch_assoc()) {
    echo "Available Room: " . $row['room_number'] . "<br>";
}
$stmt->close();
?>

				
			

3. Making a Reservation:

				
					<?php
$mysqli->begin_transaction();

try {
    $stmt = $mysqli->prepare("INSERT INTO bookings (customer_id, room_number, check_in, check_out) VALUES (?, ?, ?, ?)");
    $stmt->bind_param('iiss', $customer_id, $room_number, $check_in, $check_out);

    $customer_id = 1; // Example customer ID
    $room_number = 101; // Example room number
    $check_in = '2024-12-01';
    $check_out = '2024-12-10';

    $stmt->execute();
    $stmt->close();
    $mysqli->commit();
    echo "Reservation successful!";
} catch (Exception $e) {
    $mysqli->rollback();
    echo "Failed to make reservation: " . $e->getMessage();
}
?>

				
			

Customer Relationship Management (CRM) Systems

Case Study: A CRM Application

Overview: A CRM system manages customer interactions, sales, and support. MySQLi’s robust querying and data handling capabilities are well-suited for CRM applications.

Implementation:

1. Connecting to the Database:

				
					<?php
$mysqli = new mysqli('localhost', 'db_user', 'db_password', 'crm_db');

// Check connection
if ($mysqli->connect_error) {
    die('Connection failed: ' . $mysqli->connect_error);
}
?>

				
			

2. Retrieving Customer Information:

				
					<?php
$customer_id = 1; // Example customer ID
$query = "SELECT name, email, phone FROM customers WHERE id = ?";
$stmt = $mysqli->prepare($query);
$stmt->bind_param('i', $customer_id);
$stmt->execute();
$result = $stmt->get_result();

if ($row = $result->fetch_assoc()) {
    echo "Name: " . $row['name'] . "<br>";
    echo "Email: " . $row['email'] . "<br>";
    echo "Phone: " . $row['phone'] . "<br>";
}
$stmt->close();
?>

				
			

3. Adding Customer Notes:

				
					<?php
$stmt = $mysqli->prepare("INSERT INTO customer_notes (customer_id, note, date_added) VALUES (?, ?, ?)");
$stmt->bind_param('iss', $customer_id, $note, $date_added);

$customer_id = 1; // Example customer ID
$note = "Follow-up required regarding product feedback.";
$date_added = date('Y-m-d H:i:s');

$stmt->execute();
$stmt->close();
echo "Note added successfully!";
?>

				
			

Conclusion

MySQLi provides a versatile and secure way to interact with MySQL databases, making it suitable for a wide range of real-world applications. From e-commerce platforms and content management systems to social networking sites and booking systems, MySQLi’s features help developers build robust and efficient web applications.

Key Takeaways:

  • E-Commerce Platforms: MySQLi’s support for transactions and prepared statements ensures secure and efficient handling of orders and payments.
  • Content Management Systems (CMS): MySQLi’s object-oriented approach simplifies managing posts, categories, and user data.
  • Social Networking Sites: MySQLi supports complex queries and transactions, ideal for managing user interactions and posts.
  • Online Booking Systems: MySQLi’s features enable effective management of room availability and reservations.
  • Customer Relationship Management (CRM) Systems: MySQLi helps manage customer data and interactions efficiently.

By understanding and utilizing MySQLi’s capabilities, developers can create powerful, secure, and scalable applications that meet the needs of various industries.

Scroll to Top