Error with a PHP and MySqli query

1

I have a problem when I make a query I get an error that I do not know and I do not know why this happens to me it says something with a function called stdClass and I did this function to see if it worked but not, what can I do in this case?

function ToObject($array) {
    $object = new stdClass();
    foreach ($array as $key => $value) {
        if (is_array($value)) {
            $value = ToObject($value);
        }
        if (isset($value)) {
            $object->$key = $value;
        }
    }
    return $object;
}
  

Fatal error: Uncaught Error: Can not use object of type stdClass as   array in C: \ xampp \ htdocs \ one_2 \ application \ includes \ login.php: 69   Stack trace: # 0   C: \ xampp \ htdocs \ one_2 \ application \ system \ system_function.php (16):   require () # 1 C: \ xampp \ htdocs \ one_2 \ application \ Connection.php (12):   require_once ('C: \ xampp \ htdocs ...') # 2   C: \ xampp \ htdocs \ one_2 \ index.php (2):   require_once ('C: \ xampp \ htdocs ...') # 3 {main} thrown in   C: \ xampp \ htdocs \ one_2 \ application \ includes \ login.php on line 69

system that I'm using for this is

  

PHP-MySQLi-Database-Class:    link

Connection:

$mysqli = new mysqli($dbhost, $dbuser, $dbpassword, $dbdatabase);
$query = $mysqli->query("SET NAMES utf8");
$mysqli->set_charset("utf8");
// Connecting to DB after verfication
$db = new MysqliDb($mysqli);

This is the basic code:

$db->where ("userID", 1);
$user = $db->getOne ("user");
echo $user['user'];

This is the convention code:

========== Function for the UTF-8 caratles

 function PHP_Secure($string, $censored_words = 1) {
        global $db;
        $string = trim($string);
        $string = mysqli_real_escape_string($db, $string);
        $string = htmlspecialchars($string, ENT_QUOTES,'UTF-8');
        $string = str_replace('\r\n', " <br>", $string);
        $string = str_replace('\n\r', " <br>", $string);
        $string = str_replace('\r', " <br>", $string);
        $string = str_replace('\n', " <br>", $string);
        $string = str_replace('&amp;#', '&#', $string);
        $string = stripslashes($string);
        if ($censored_words == 1) {
            global $config;
            $censored_words = @explode(",", $config['censored_words']);
            foreach ($censored_words as $censored_word) {
                $censored_word = trim($censored_word);
                $string        = str_replace($censored_word, '****', $string);
            }
        }
        return $string;
    }

========== Login function

    function system_login() {

        global $con;
        if (@$_COOKIE["muser"]!='') {
        @header("Location:./");
        exit('<meta http-equiv="Refresh" content="0;url=./">');
        }
        if (!empty($_POST)) {

    $username        = PHP_Secure($_POST['username']);
            $password        = PHP_Secure($_POST['password']);
            $password_hashed = sha1($password);
        // EN ESTA LINIA ME DA EL ERROR
            $db->where("(username = ?)", array(
                $username
            ));
// EN ESTA LINIA ME DA EL ERROR
            $db->where("password", $password_hashed);
            $login = $db->getOne(T_USERS);
            if (!empty($login)) {
                if ($login->active == 0) {
                    $errors = 'ERROR';
                } else {
                    $session_id          = sha1(rand(11111, 99999)) . time() . md5(microtime());
                    $insert_data         = array(
                        'user_id' => $login->id,
                        'session_id' => $session_id,
                        'time' => time()
                    );
                    $insert              = $db->insert(T_SESSIONS, $insert_data);
                    $_SESSION['user_id'] = $session_id;
                    setcookie("user_id", $session_id, time() + (10 * 365 * 24 * 60 * 60));
                    $pt->loggedin = true;
                    if (!empty($_GET['to'])) {
                        $site_url = $_GET['to'];
                    }
                    header("Location: $site_url");
                    exit();
                }
        }
    }
    
asked by sode 17.05.2018 в 05:08
source

1 answer

0

The problem is that PHP does not know what the variable $ db is. You must make an include or require your connection file up inside the function system_login () or go through parameters system_login ($ db); when you call her.

  function system_login() {

       //Esto deberia servir
       require __DIR__ . "/directorio/hacia/conexion.php"; //archivo que hace la conexion

        global $con;
        if (@$_COOKIE["muser"]!='') {
        @header("Location:./");
        exit('<meta http-equiv="Refresh" content="0;url=./">');
        }
        if (!empty($_POST)) {

    $username        = PHP_Secure($_POST['username']);
            $password        = PHP_Secure($_POST['password']);
            $password_hashed = sha1($password);
        // EN ESTA LINIA ME DA EL ERROR
            $db->where("(username = ?)", array(
                $username
            ));
// EN ESTA LINIA ME DA EL ERROR
            $db->where("password", $password_hashed);
            $login = $db->getOne(T_USERS);
            if (!empty($login)) {
                if ($login->active == 0) {
                    $errors = 'ERROR';
                } else {
                    $session_id          = sha1(rand(11111, 99999)) . time() . md5(microtime());
                    $insert_data         = array(
                        'user_id' => $login->id,
                        'session_id' => $session_id,
                        'time' => time()
                    );
                    $insert              = $db->insert(T_SESSIONS, $insert_data);
                    $_SESSION['user_id'] = $session_id;
                    setcookie("user_id", $session_id, time() + (10 * 365 * 24 * 60 * 60));
                    $pt->loggedin = true;
                    if (!empty($_GET['to'])) {
                        $site_url = $_GET['to'];
                    }
                    header("Location: $site_url");
                    exit();
                }
        }
    }
    
answered by 17.05.2018 в 19:23