Database Installation for PHP Projects | MySQLi Tutorial - Learn with VOKS
Back

Database Installation for PHP Projects


Databases are essential for storing and managing data in dynamic websites. The most commonly used database with PHP is MySQL or its fork MariaDB.


What is a Database?

  • A database stores data in a structured format (tables, rows, and columns).
  • Examples: User accounts, products, blog posts.
  • PHP interacts with the database to store, retrieve, update, and delete information.


Installing MySQL / MariaDB

There are two main approaches: using an all-in-one package like XAMPP/WAMP/MAMP or installing manually.


Option 1: Using XAMPP (Recommended for Beginners)

XAMPP includes:

  • Apache (Web Server)
  • MySQL / MariaDB (Database)
  • PHP & phpMyAdmin (Database GUI)

Steps:

  1. Download XAMPP: https://www.apachefriends.org/download.html
  2. Install XAMPP on your system.
  3. Open the XAMPP Control Panel.
  4. Start Apache and MySQL services.
  5. Open http://localhost/phpmyadmin in your browser → phpMyAdmin is your database manager.


Option 2: Manual Installation

Windows:

  1. Install MySQL Community Server: https://dev.mysql.com/downloads/mysql
  2. During setup, set a root password.
  3. Install PHP (if not using XAMPP/WAMP).
  4. Verify installation:
mysql -u root -p
  1. Connect PHP with MySQL using mysqli or PDO.

Linux (Ubuntu/Debian):

sudo apt update
sudo apt install mysql-server
sudo mysql_secure_installation
sudo apt install php php-mysql
  • Start MySQL server:
sudo service mysql start

Mac (Using Homebrew or MAMP):

brew install mysql
brew services start mysql
  • MAMP includes MySQL, PHP, and Apache for easy setup.


Creating Your First Database

After installation, you can create a database via phpMyAdmin or MySQL CLI.

Using phpMyAdmin:

  1. Open http://localhost/phpmyadmin
  2. Click Databases → Enter database name (e.g., my_website) → Click Create

Using MySQL Command Line:

CREATE DATABASE my_website;
USE my_website;

CREATE TABLE users (
    id INT AUTO_INCREMENT PRIMARY KEY,
    name VARCHAR(50) NOT NULL,
    email VARCHAR(50) NOT NULL,
    age INT(3)
);


Connecting PHP to the Database

Using MySQLi (Procedural):

<?php
$conn = mysqli_connect("localhost", "root", "", "my_website");

if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
}
echo "Database connected successfully!";
?>


Using PDO (Recommended):

<?php
try {
    $conn = new PDO("mysql:host=localhost;dbname=my_website", "root", "");
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    echo "Database connected successfully using PDO!";
} catch(PDOException $e) {
    echo "Connection failed: " . $e->getMessage();
}
?>


Best Practices for Database Installation

  1. Set a strong root password during installation.
  2. Use phpMyAdmin or another GUI for easy management.
  3. Create separate users for different applications instead of using root.
  4. Secure your database by disabling remote root login if not needed.
  5. Use PDO or MySQLi prepared statements to prevent SQL injection.


What this example does:

  • Connects to MySQL
  • Creates a database if it doesn’t exist
  • Creates a users table inside the database
  • Ready for storing user data


Practice Example: Create and Connect to a Database

Example Code:
<?php
// Connect to MySQL
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "my_website";

// 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 $dbname";
if ($conn->query($sql) === TRUE) {
    echo "Database '$dbname' created successfully!";
} else {
    echo "Error creating database: " . $conn->error;
}

// Connect to the new database
$conn->select_db($dbname);

// Create table
$sql = "CREATE TABLE IF NOT EXISTS users (
    id INT AUTO_INCREMENT PRIMARY KEY,
    name VARCHAR(50) NOT NULL,
    email VARCHAR(50) NOT NULL
)";
if ($conn->query($sql) === TRUE) {
    echo "<br>Table 'users' created successfully!";
} else {
    echo "<br>Error creating table: " . $conn->error;
}

$conn->close();
?>
MySQLi
PHP-MySQL Installation Database Installation for PHP Projects
All Courses
Advance AI Bootstrap C C++ Computer Vision Content Writing CSS Cyber Security Data Analysis Deep Learning Email Marketing Excel Figma HTML Java Script Machine Learning MySQLi Node JS PHP Power Bi Python Python for AI Python for Analysis React React Native SEO SMM SQL