0

There is such an error. $db. There is such an unpleasant error. How can this be fixed. I don’t understand how to solve this error, although I asked different people many times, but they did not explain it clearly.Please explain the solution to this problem! This is where the variable is declared:

<?php

/**
 * 
 */
class GetConnection
{
     @return 

    public static function GetConnection()
    {


        $paramsPath = ROOT . '/config/db_params.php';
        $params = include($paramsPath);


        $dsn = "mysql:host={$params['host']};dbname={$params['dbname']}";
        $db = new PDO($dsn, $params['user'], $params['password']);


        $db->exec("set names utf8");

        return $db;
    }

}

Here is the file to which the error is sent:

<?php


class Category
{


    public static function getCategoriesList()
    {
        require_once(getConection)

        $db = include GetConnection::getConnection;

        $result = $db->query('SELECT id, name FROM category WHERE status = "1" ORDER BY sort_order, name ASC');


        $i = 0;
        $categoryList = array();
        while ($row = $result->fetch()) {
            $categoryList[$i]['id'] = $row['id'];
            $categoryList[$i]['name'] = $row['name'];
            $i++;
        }
        return $categoryList;
    }


    public static function getCategoriesListAdmin()
    {

        $db = GetConnection::getConnection();

        $result = $db->query('SELECT id, name, sort_order, status FROM category ORDER BY sort_order ASC');


        $categoryList = array();
        $i = 0;
        while ($row = $result->fetch()) {
            $categoryList[$i]['id'] = $row['id'];
            $categoryList[$i]['name'] = $row['name'];
            $categoryList[$i]['sort_order'] = $row['sort_order'];
            $categoryList[$i]['status'] = $row['status'];
            $i++;
        }
        return $categoryList;
    }


    public static function deleteCategoryById($id)
    {

        $db = GetConnection::getConnection();


        $sql = 'DELETE FROM category WHERE id = :id';


        $result = $db->prepare($sql);
        $result->bindParam(':id', $id, PDO::PARAM_INT);
        return $result->execute();
    }

    public static function updateCategoryById($id, $name, $sortOrder, $status)
    {

        $db = GetConnection::getConnection();

        $sql = "UPDATE category
            SET 
                name = :name, 
                sort_order = :sort_order, 
                status = :status
            WHERE id = :id";


        $result = $db->prepare($sql);
        $result->bindParam(':id', $id, PDO::PARAM_INT);
        $result->bindParam(':name', $name, PDO::PARAM_STR);
        $result->bindParam(':sort_order', $sortOrder, PDO::PARAM_INT);
        $result->bindParam(':status', $status, PDO::PARAM_INT);
        return $result->execute();
    }


    public static function getCategoryById($id)
    {
        $db = GetConnection::getConnection();

        $sql = 'SELECT * FROM category WHERE id = :id';


        $result = $db->prepare($sql);
        $result->bindParam(':id', $id, PDO::PARAM_INT);


        $result->setFetchMode(PDO::FETCH_ASSOC);

        $result->execute();

        return $result->fetch();
    }


    public static function getStatusText($status)
    {
        switch ($status) {
            case '1':
                return 'Отображается';
                break;
            case '0':
                return 'Скрыта';
                break;
        }
    }

    public static function createCategory($name, $sortOrder, $status)
    {

        $db = GetConnection::getConnection();


        $sql = 'INSERT INTO category (name, sort_order, status) '
                . 'VALUES (:name, :sort_order, :status)';


        $result = $db->prepare($sql);
        $result->bindParam(':name', $name, PDO::PARAM_STR);
        $result->bindParam(':sort_order', $sortOrder, PDO::PARAM_INT);
        $result->bindParam(':status', $status, PDO::PARAM_INT);
        return $result->execute();
    }

}
  • 3
    You're missing `;` at the end of this line: `require_once(getConection)`, which most likely is an invalid call by itself, unless you have a constant called `getConection` that contains the file path you're trying to require. – M. Eriksson May 11 '20 at 16:19
  • 2
    This: `$db = include GetConnection::getConnection;` is also wrong. You are doing correct further down in the same class though: `$db = GetConnection::getConnection();`. – M. Eriksson May 11 '20 at 16:22

0 Answers0