# Database Connection Fix - Quick Guide

## 🔴 Error: "Database connection not set"

This error occurs when the application can't connect to the database. Follow these steps to fix it:

## ✅ Step 1: Check .env File

Make sure you have a `.env` file in the root directory:

```bash
# If .env doesn't exist, copy from example
cp .env.example .env
```

## ✅ Step 2: Configure Database Credentials

Edit `.env` file and set your database credentials:

```env
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=erp_system
DB_USERNAME=root
DB_PASSWORD=your_password_here
```

### For production server (based on your error path):
```env
DB_HOST=localhost
DB_DATABASE=admin254_inventory  # or your actual database name
DB_USERNAME=admin254_inventory  # or your actual username
DB_PASSWORD=your_database_password
```

## ✅ Step 3: Create Database (if not exists)

```sql
CREATE DATABASE erp_system CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
```

Or use your hosting control panel (cPanel, Plesk, etc.) to create the database.

## ✅ Step 4: Import Database Schema

```bash
mysql -u root -p erp_system < project.sql
```

Or using your credentials:
```bash
mysql -u your_username -p your_database_name < project.sql
```

## ✅ Step 5: Verify Configuration

Run the diagnostic tool:

```bash
php check_database.php
```

This will:
- ✓ Check if .env exists
- ✓ Verify database credentials
- ✓ Test database connection
- ✓ Check if tables exist
- ✓ Verify admin user

## 🔧 Quick Diagnostics

### Test 1: Check if database exists
```bash
mysql -u root -p -e "SHOW DATABASES LIKE 'erp_system';"
```

### Test 2: Check if you can connect
```bash
mysql -u root -p erp_system -e "SELECT 1;"
```

### Test 3: Check if users table exists
```bash
mysql -u root -p erp_system -e "SHOW TABLES LIKE 'users';"
```

## 🚨 Common Issues & Solutions

### Issue 1: .env file permissions (Linux/Server)
```bash
chmod 644 .env
chown www-data:www-data .env  # or your web server user
```

### Issue 2: MySQL service not running
**Windows:**
```powershell
net start MySQL
```

**Linux:**
```bash
sudo systemctl start mysql
# or
sudo service mysql start
```

### Issue 3: Database doesn't exist
```sql
CREATE DATABASE erp_system;
GRANT ALL ON erp_system.* TO 'root'@'localhost';
FLUSH PRIVILEGES;
```

### Issue 4: Wrong credentials
Double-check:
1. Username exists in MySQL
2. Password is correct
3. User has permissions on the database

Test with:
```bash
mysql -u your_username -p
# Enter password when prompted
```

## 📝 Production Server Setup (cPanel/Plesk)

1. **Create Database:**
   - Go to MySQL Databases
   - Create new database (e.g., `admin254_inventory`)
   - Note the full database name (usually prefixed: `admin254_inventory`)

2. **Create Database User:**
   - Create user
   - Set strong password
   - Add user to database with ALL PRIVILEGES

3. **Update .env:**
   ```env
   DB_HOST=localhost
   DB_DATABASE=admin254_inventory
   DB_USERNAME=admin254_dbuser
   DB_PASSWORD=your_strong_password
   ```

4. **Import SQL:**
   - Use phpMyAdmin
   - Or command line: `mysql -u admin254_dbuser -p admin254_inventory < project.sql`

5. **Set Permissions:**
   ```bash
   chmod 644 .env
   chmod -R 755 storage/
   chmod -R 755 bootstrap/cache/
   ```

## 🎯 After Fixing

Once configured, you should be able to login with:
- **Username:** admin
- **Password:** admin123

## 📞 Still Having Issues?

Run the diagnostic tool for detailed error messages:
```bash
php check_database.php
```

The output will show exactly what's wrong and how to fix it.
