Warning: mysqli_query () expects parameter 1 to be mysqli, null given in /home1/j8f5w8o7/nepe.smeext-carding.com/pieces/inc.php on line 26

0

I have 2 problems that appear at the beginning of my site, and they are the following:

Warning: mysqli_query() expects parameter 1 to be mysqli, null given in
/home1/j8f5w8o7/nepe.smeext-carding.com/pieces/inc.php on line 26

Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, null given in 
/home1/j8f5w8o7/nepe.smeext-carding.com/pieces/inc.php on line 27

This is my code:

<?php
session_start();
ob_start();

$display = new display();
$user = new user();

DEFINE("localhost", "");
DEFINE("j8f5w8o7_finix", "");
DEFINE("yael2204", "");
DEFINE("j8f5w8o7_finix", "");

$sql = new mysqli(host, username, password, database);

function __destruct()
{
    ob_clean();
}

class settings
{
    public static function website($data)
    {
        global $sql;

        $website = mysqli_query($sql, 'SELECT * FROM merchant LIMIT 1');
        $fwebsite = mysqli_fetch_array($website);

        return $fwebsite[$data];
    }
}

class display {
    public static function success($msg)
    {
        echo('<div class="alert alert-success role="alert" style="text-align: center;">'.$msg.'</div>');
    }
    public static function error($msg)
    {
        echo('<div class="alert alert-danger role="alert" style="text-align: center;">'.$msg.'</div>');
    }
}

class user {
    function IsLogged()
    {
        global $sql;

        /* If session exists. */

        if(!isset($_SESSION['auth'])){
            header('Location: signin.php');
            exit();
        }

        /* Additional check.If session is null or user does not exists. */

        if(isset($_SESSION['auth'])) {
            if(is_numeric($_SESSION['auth']) && !empty($_SESSION['auth'])) {
                $query = mysqli_query($sql, 'SELECT UserName FROM users WHERE UserID = "'.$_SESSION['auth'].'"');

                if(mysqli_num_rows($query) == 0) {
                    unset($_SESSION['auth']);
                    header('Location: signin.php');
                    exit();
                }
            }
        }
    }

    function IsBanned()
    {
        $banned = $this->GetData('UserBanned');

        if($banned == 1) {
            header('Location: ./index.php');
            exit();
        }
    }

    function IsAdmin()
    {
        if($this->GetData('UserAdmin') == 0) {
            header('Location: ./index.php');
            exit();
        }
    }

    function HasMembership()
    {
        if($this->GetData('UserMembership') == 0) {
            header('Location: ./purchase.php');
            exit();
        }
    }

    function GetData($data)
    {
        global $sql;

        if(isset($_SESSION['auth'])) {
            $id = $_SESSION['auth'];

            if(is_numeric($id)) {
                $query = mysqli_query($sql, 'SELECT '.$data.' FROM users WHERE UserID = "'.$id.'"');
                $row = mysqli_fetch_array($query);
                return $row[$data];
            }
        }
    }
}
    
asked by Sergio Yael Alvarado 30.05.2017 в 08:57
source

1 answer

1

You are obviously trying to define constants with the credentials to connect to the database.

It can be done, but it does not make much sense, since in PHP the constants are a kind of global variables.

  

Just like the superglobal , access to a constant is global. He   can access constants from any site in the script without   import from where. For more information on access, read the manual   in the section access to variables .

     

❯ Source: Constants in the PHP Manual

So, applying good programming practices, a constant would be defined to be used in various parts of the code ... here what would be interesting to use in various parts of the code is the connection itself ( for example through a connection class), so define constants in this case.

If you want to use constants anyway, the correct way would be:

DEFINE("HOST", "localhost");
DEFINE("USERNAME", "j8f5w8o7_finix");
DEFINE("PASSWORD", "yael2204");
DEFINE("DATABASE", "j8f5w8o7_finix");

$mysqli = new mysqli(HOST, USERNAME, PASSWORD, DATABASE);

You will see that I have capitalized them, because the naming convention requires it (see previous link in the Manual).

For the connection to the database, it is advisable to have a class dedicated to the connection that returns a connection object, whenever you need it. To connect to the bd you can define simple variables since you do not need these variables to have a global scope, they will also occupy less memory than a constant.

Example:

$host="localhost";
$username="j8f5w8o7_finix";
$password="yael2204";
$database="j8f5w8o7_finix";
$mysqli = new mysqli($host, $username, $password, $database);

Having a class that returns the connection object $mysqli the same would use local credentials and would return the connection which is what interests you. The less public and less global your credentials are, the better. So using DEFINE for connection credentials could be considered a bad practice.

I've also called the connection $mysqli because usually the connection object is called like this, or $ con, or $ bd ... it's a matter of taste and ... of applying naming convention rules, since $sql is usually used for queries such as SELECT ... FROM ... , INSERT INTO ... , etc.

    
answered by 30.05.2017 в 12:40