← CodePrimer
PHP
Beginner
Database
require db.php connection
How do I reuse one DB connection file?
require/include pattern.
<?php
require __DIR__ . '/db.php'; // provides $pdo
$stmt = $pdo->query('SELECT COUNT(*) AS c FROM students');
echo $stmt->fetch()['c'];
Notes
Centralize credentials in one non-public config file.
Keywords: require · include · db.php · connection
Related
HTML
HTML headings h1–h6
How do I create page headings?
CSS
Three ways to apply CSS
What are inline, internal, and external CSS?
PHP
Embed PHP inside HTML
How do I mix PHP and HTML?
SQL
CREATE DATABASE
How do I create a MySQL database?
HTML
Paragraphs and line breaks
How do I break text into paragraphs?
CSS
CSS box model
Why is my element wider than expected?