Essential Guide to MariaDB Mastering APIs and Practical App Example

Introduction to MariaDB

MariaDB is an open-source relational database management system (RDBMS) that is widely used for its high performance, security, and compatibility with MySQL. It is popular among developers for building robust and scalable applications.

MariaDB API Examples

MariaDB provides a comprehensive and flexible set of APIs to interact with the database. Below are several examples demonstrating the use of these APIs.

Connecting to MariaDB

  <?php
  $mysqli = new mysqli("localhost", "username", "password", "database");

  if ($mysqli->connect_error) {
      die("Connection failed: " . $mysqli->connect_error);
  }
  echo "Connected successfully";
  ?>

Creating a Table

  <?php
  $sql = "CREATE TABLE Users (
      id INT AUTO_INCREMENT PRIMARY KEY,
      username VARCHAR(255) NOT NULL,
      email VARCHAR(255),
      created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
  )";

  if ($mysqli->query($sql) === TRUE) {
      echo "Table Users created successfully";
  } else {
      echo "Error creating table: " . $mysqli->error;
  }
  ?>

Inserting Data

  <?php
  $sql = "INSERT INTO Users (username, email) VALUES ('john_doe', 'john@example.com')";

  if ($mysqli->query($sql) === TRUE) {
      echo "New record created successfully";
  } else {
      echo "Error: " . $sql . "<br>" . $mysqli->error;
  }
  ?>

Querying Data

  <?php
  $sql = "SELECT id, username, email FROM Users";
  $result = $mysqli->query($sql);

  if ($result->num_rows > 0) {
      while($row = $result->fetch_assoc()) {
          echo "id: " . $row["id"]. " - Name: " . $row["username"]. " - Email: " . $row["email"]. "<br>";
      }
  } else {
      echo "0 results";
  }
  ?>

Updating Data

  <?php
  $sql = "UPDATE Users SET email='johndoe_new@example.com' WHERE id=1";

  if ($mysqli->query($sql) === TRUE) {
      echo "Record updated successfully";
  } else {
      echo "Error updating record: " . $mysqli->error;
  }
  ?>

Deleting Data

  <?php
  $sql = "DELETE FROM Users WHERE id=1";

  if ($mysqli->query($sql) === TRUE) {
      echo "Record deleted successfully";
  } else {
      echo "Error deleting record: " . $mysqli->error;
  }
  ?>

Practical App Example

Let’s create a simple PHP web application to manage a list of users.

index.php

  <!DOCTYPE html>
  <html>
  <head>
      <title>User Management</title>
  </head>
  <body>
      <h1>User Management</h1>
      <?php
      $mysqli = new mysqli("localhost", "username", "password", "database");

      if ($mysqli->connect_error) {
          die("Connection failed: " . $mysqli->connect_error);
      }

      $sql = "SELECT id, username, email FROM Users";
      $result = $mysqli->query($sql);

      if ($result->num_rows > 0) {
          echo "<table>
              <tr>
              <th>ID</th>
              <th>Username</th>
              <th>Email</th>
              </tr>";
          while($row = $result->fetch_assoc()) {
              echo "<tr>
                  <td>" . $row["id"]. "</td>
                  <td>" . $row["username"]. "</td>
                  <td>" . $row["email"]. "</td>
                  </tr>";
          }
          echo "</table>";
      } else {
          echo "0 results";
      }

      $mysqli->close();
      ?>
  </body>
  </html>

With these snippets, you have a basic user management application using MariaDB for data storage.

Hash: e08f4c9c36148019458d78d4baa84cbfa4beeb6e36be36c96f74c8655723b1ba

Leave a Reply

Your email address will not be published. Required fields are marked *