Common issues and solutions when using PDOdb.
Problem: PDO extension not installed.
Solution:
# Ubuntu/Debian
sudo apt-get install php8.4-mysql php8.4-pgsql php8.4-sqlite3
# Check installed extensions
php -m | grep pdoProblem: Database server not running or wrong credentials.
Solution:
// Test connection
try {
$db = new PdoDb('mysql', [
'host' => 'localhost',
'username' => 'user',
'password' => 'pass',
'dbname' => 'mydb'
]);
if ($db->ping()) {
echo "Connected successfully\n";
}
} catch (\Exception $e) {
echo "Connection failed: {$e->getMessage()}\n";
}Problem: SQLite compiled without JSON1 extension.
Solution:
# Check JSON support
sqlite3 :memory: "SELECT json_valid('{}')"
# If error, SQLite needs recompilation with JSON supportProblem: Using OFFSET without LIMIT in SQLite.
Solution:
// ❌ Doesn't work in SQLite
$db->find()->from('users')->offset(10)->get();
// ✅ Works
$db->find()
->from('users')
->limit(20)
->offset(10)
->get();Problem: Table or column doesn't exist.
Solution:
// Check if table exists
$exists = $db->find()->table('users')->tableExists();
if (!$exists) {
// Create table
$db->rawQuery('CREATE TABLE users (...)');
}Problem: Missing indexes.
Solution:
-- Create index
CREATE INDEX idx_users_email ON users(email);
-- Check with EXPLAIN// Analyze query
$plan = $db->find()
->from('users')
->where('email', 'alice@example.com')
->explain();
print_r($plan);Problem: Loading too much data.
Solution:
// ✅ Use batch processing
foreach ($db->find()
->from('users')
->orderBy('id')
->batch(1000) as $batch) {
processBatch($batch);
}
// ✅ Use cursor for streaming
foreach ($db->find()
->from('users')
->cursor() as $user) {
processUser($user);
}Problem: Nested transactions.
Solution:
// Check transaction status
if (!$db->inTransaction()) {
$db->startTransaction();
}
// Or use transaction callback
$result = $db->transaction(function($db) {
return $db->find()->table('users')->insert(['name' => 'Alice']);
});- Always use prepared statements (automatic in PDOdb)
- Use transactions for multiple operations
- Create indexes for frequently queried columns
- Use LIMIT for all SELECT queries
- Monitor slow queries with EXPLAIN
- Check error messages carefully
- Enable query logging
- Use EXPLAIN to analyze queries
- Check database-specific documentation
- Review PDOdb examples
- Common Patterns - Reusable patterns
- Real-World Examples - Complete apps
- Best Practices - Security and performance