Home > Database > Mysql Tutorial > How to Properly Access a MySQLi Database Connection from Different Classes in PHP?

How to Properly Access a MySQLi Database Connection from Different Classes in PHP?

Barbara Streisand
Release: 2024-12-08 15:12:11
Original
1022 people have browsed it

How to Properly Access a MySQLi Database Connection from Different Classes in PHP?

Using MySQLi from Another Class in PHP: A Detailed Breakdown

In this query, the user has upgraded MySQL to MySQLi after transitioning from PHP 5.6 to 7.0. After following the guide "Using MySQLi in other classes," they encountered internal server error 500 when attempting to access a database connection from a different class.

To understand the error, let's examine the user's PHP code. They have three classes: a Database class for establishing the MySQLi connection, a MyAPI class where they try to access this connection, and a separate function where they make the actual database query.

Error Analysis

The problem arises from several fundamental issues in the code:

  1. Incorrect Class Hierarchy: Extending the User class from the Database class is an incorrect approach.
  2. Redundant Database Class: The Database class essentially serves no practical purpose and unnecessarily complicates the code.

Solution

To address these issues, the following steps are recommended:

  1. Remove the Database Class: Since it offers no significant functionality, the Database class should be eliminated.
  2. Create a Single $db Instance: Establish a single instance of $db using vanilla MySQLi.
  3. Pass $db as a Constructor Parameter: In every class that requires a database connection, pass the $db instance as a constructor parameter.

Here's an improved PHP code example:

database.php

<?php
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$db = new mysqli("localhost", "DBUserName", "UserPassword", "SelectedDB");
$db->set_charset('utf8mb4');
Copy after login

myapi.php

<?php
class MyAPI
{
    protected $db;

    public function __construct($db, $request_uri, $postData, $origin)
    {
        $this->db = $db;
    }

    public function getUser($id)
    {
        $sql = "SELECT * FROM users where>
Copy after login

app.php

<?php
# require_once 'Database.php';
# require_once 'myapi.php';
require 'vendor/autoload.php'; // autoloading is a must

$api = new MyAPI($db, $request_uri, $postData, $origin);
$user = $api->getUser($_POST['id']);
Copy after login

With these modifications, the user should be able to establish a proper MySQLi connection and perform database queries from different classes.

The above is the detailed content of How to Properly Access a MySQLi Database Connection from Different Classes in PHP?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template