Making a Reusable Connection Script
Build a Reusable DB Connection
1
Cut the Connection Script from MySQL.php
$conn = new mysqli('localhost', 'root', 'root', 'phpclass_yourname');
2
Save as inc/dbConnect.php
Wrap in <?php ... ?> tags so it parses correctly when included.
3
Add Error Checking
if ($conn->connect_error) { die('Connection failed: ' . $conn->connect_error); }
4
Include from Any Page
<?php require_once 'inc/dbConnect.php'; ?> at the top of pages that need DB access.
Build Back-End Skills at Noble Desktop
Noble Desktop's Full-Stack Web Development Certificate teaches modern back-end development — concepts that carry across PHP, Node.js, and Python.
Delve into this comprehensive tutorial on PHP and MySQL, covering vital topics such as error checking, the creation of includes, and result sorting techniques.