To set up MySQL in the XAMPP PHP environment with a database, users, and tables, follow these steps:
✅ Step 1: Start XAMPP Services
-
Open XAMPP Control Panel.
-
Start Apache and MySQL services.
-
Click Admin next to MySQL to launch phpMyAdmin in your browser.
✅ Step 2: Create a Database in phpMyAdmin
-
In phpMyAdmin, click "New" on the left sidebar.
-
Under "Create database", enter a name (e.g.,
mydb
) and choose collation likeutf8mb4_general_ci
. -
Click Create.
✅ Step 3: Create a User with a Password
-
Go to the "User accounts" tab in phpMyAdmin.
-
Click "Add user account".
-
Enter:
-
Username: e.g.,
myuser
-
Host name:
localhost
-
Password: your desired password
-
-
Under Database for user, choose "Grant all privileges on database" and select your database (
mydb
). -
Scroll down and click Go.
✅ Step 4: Create Tables
-
Click your database name (
mydb
) from the sidebar. -
Under "Create table", enter:
-
Table name: e.g.,
users
-
Number of columns: e.g., 3
-
-
Click Go, then define columns like:
-
id
– INT, Primary Key, AUTO_INCREMENT -
username
– VARCHAR(50) -
email
– VARCHAR(100)
-
-
Click Save.
✅ Step 5: Connect to the Database in PHP (Optional)
If you're building a PHP app, here’s a sample connection script:
<?php
$servername = "localhost";
$username = "myuser";
$password = "yourpassword";
$dbname = "mydb";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
?>
🧠 Tips
-
phpMyAdmin is a friendly GUI but you can also run raw SQL via the "SQL" tab.
-
For example, creating a user and database via SQL:
CREATE DATABASE mydb;
CREATE USER 'myuser'@'localhost' IDENTIFIED BY 'yourpassword';
GRANT ALL PRIVILEGES ON mydb.* TO 'myuser'@'localhost';
FLUSH PRIVILEGES;
Let me know if you want a script to automatically generate users and tables, or want to do it via CLI.
No comments:
Post a Comment