Overview
The Refugio system uses PDO (PHP Data Objects) for database connectivity, providing a secure and flexible interface for database operations. The system supports both PostgreSQL and MySQL databases.Connection File Structure
The database connection is established in theconexion.php file, which must be included at the beginning of every PHP script that requires database access.
File Location
Connection Setup
Theconexion.php file typically follows this structure:
Basic Structure
PDO Configuration Options
Essential PDO Attributes
The connection uses the following PDO attributes for security and performance:PDO::ATTR_ERRMODE
- Proper error handling with try-catch blocks
- Prevents silent failures
- Allows for custom error logging
PDO::ATTR_DEFAULT_FETCH_MODE
- Clean array access using column names
- No numeric indexes
- More readable code
PDO::ATTR_EMULATE_PREPARES
- Better SQL injection protection
- Database-level query optimization
- Type safety for parameters
PDO::ATTR_PERSISTENT
- Better for shared hosting environments
- Prevents connection pool exhaustion
- More predictable behavior
true for high-traffic sites with dedicated servers.
Database-Specific Configurations
PostgreSQL Connection
- Native ENUM support
- Better JSONB handling
- Advanced indexing options
- Full ACID compliance
MySQL Connection
charset=utf8mb4in DSN for emoji supportPDO::MYSQL_ATTR_INIT_COMMANDfor proper collation- Uses VARCHAR with CHECK constraints instead of ENUM
Security Best Practices
1. Prepared Statements
Always use prepared statements with parameter binding:2. Parameter Binding Methods
Two ways to bind parameters: Named Parameters (Recommended):3. Parameter Types
Specify data types for better security:PDO::PARAM_INT- Integer valuesPDO::PARAM_STR- String values (default)PDO::PARAM_BOOL- Boolean valuesPDO::PARAM_NULL- NULL values
4. Error Handling
Never expose database errors to users:5. Configuration Security
Never commit conexion.php to version control! Use environment variables or a separate config file:Usage Examples
Including the Connection
Every PHP file that needs database access must includeconexion.php:
Simple Query
Prepared Statement with Parameters
Insert with Transaction
Fetch Multiple Rows
Connection Verification
The system includes a verification scriptverificar_mysql.php that checks:
- Database connectivity
- Database selection
- Table existence
- Sample data presence
- Password hashing
- User permissions
Troubleshooting
Connection Refused
Error:SQLSTATE[08006] [7] could not connect to server
Solutions:
- Check if database server is running
- Verify host and port in connection string
- Check firewall settings
Authentication Failed
Error:SQLSTATE[28P01] authentication failed
Solutions:
- Verify username and password
- Check user permissions
- Ensure user has access to the database
Database Not Found
Error:SQLSTATE[3D000] database "refugio" does not exist
Solutions:
UTF-8 Encoding Issues
Problem: Special characters not displaying correctly Solutions:Environment-Specific Configurations
Development Environment
Production Environment
Performance Optimization
Connection Pooling (Production)
Query Optimization
Prepared Statement Reuse
Related Documentation
- Database Schema - Complete schema documentation
- Functions Reference - Database functions using PDO
- Security Guide - Security best practices