1

I wrote some code, and I'm calling(require function) .php file which will connect to MySQL base. That file(not file for MySQL connection) I call into other because I want open one connection one time, and close it one time. Error says

Undefined variable: _hsync_konekcija in C:\Program Files\WAMP\www\hsync_hsync_scripts_hsync_pristup.php on line 21

_hsync_pristup.php file

<?php

require('_hsync_scripts/_hsync_baza.php');

function _hsync_pristup()
{
    session_start();

    if(isset($_COOKIE['_hsync_prijavljen'])) $_hsync_id = $_COOKIE['_hsync_prijavljen']; // PRIJAVLJEN
    else if(isset($_SESSION['_hsync_sess_prijavljen'])) $_hsync_id = $_SESSION['_hsync_sess_prijavljen']; // PRIJAVLJEN
    else
    {
        $_hsync_pristup_info = array(
            '_hsync_pristup' => 0
        );

        $_hsync_konekcija->close();
        return ($_hsync_pristup_info);
    }

    /* THIS LINE */ $_hsync_statment = $_hsync_konekcija->prepare("SELECT Zakljucan, Ime, Skin FROM $_hsync_usr WHERE ID = ?"); // LINE 21
    $_hsync_statment->bind_param("i", $_hsync_id);
    $_hsync_statment->execute();
    $_hsync_rezultat = $_hsync_statment->get_result();

    if($_hsync_rezultat->num_rows == 0) // OBIRSAN RAČUN
    {
        $_hsync_pristup_info = array(
            '_hsync_pristup' => -1
        );

        session_unset();
        session_destroy();
        setcookie("_hsync_prijavljen", null, -1, "/");

        $_hsync_statment->close();
        $_hsync_rezultat->close();
        $_hsync_konekcija->close();
        return ($_hsync_pristup_info);
    }

    $_hsync_podatci = $_hsync_rezultat->fetch_assoc();
    if($_hsync_podatci["Zakljucan"] != 0) // ZAKLJUČAN RAČUN
    {
        session_unset();
        session_destroy();
        setcookie("_hsync_zakljucan", $_hsync_id, time() + 8, "/");
        setcookie("_hsync_zakljucan_zap", rand(6, 16), time() + 8, "/");
        setcookie("_hsync_prijavljen", null, -1, "/");

        $_hsync_pristup_info = array(
            '_hsync_pristup' => -2
        );

        $_hsync_statment->close();
        $_hsync_rezultat->close();
        $_hsync_konekcija->close();
        return ($_hsync_pristup_info);
    }
    else
    {
        $_hsync_ime = $_hsync_podatci['Ime'];
        $_hsync_skin = $_hsync_podatci['Skin'];
    }

    $_hsync_statment = $_hsync_konekcija->prepare("SELECT ServerID FROM $_hsync_srv_online WHERE ID = ?");
    $_hsync_statment->bind_param("i", $_hsync_id);
    $_hsync_statment->execute();
    $_hsync_rezultat = $_hsync_statment->get_result();

    if($_hsync_rezultat->num_rows != 0) // PRIJAVLJEN NA GAME SERVERU
    {
        $_hsync_podatci = $_hsync_rezultat->fetch_assoc();
        $_hsync_serverid = $_hsync_podatci["ServerID"];

        session_unset();
        session_destroy();
        setcookie("_hsync_prijavljen", null, -1, "/");
        setcookie("_hsync_online", $_hsync_id, time() + 8, "/");
        setcookie("_hsync_online_id", $_hsync_serverid, time() + 8, "/");

        $_hsync_pristup_info = array(
            '_hsync_pristup' => -3
        );

        $_hsync_statment->close();
        $_hsync_rezultat->close();
        $_hsync_konekcija->close();
        return ($_hsync_pristup_info);
    }

    if(isset($_COOKIE['_hsync_zabrana'])) return (-4); // IMA ZABRANU PRISTUPA
    else // NEMA ZABRANU
    {
        $_hsync_statment = $_hsync_konekcija->prepare("SELECT ID FROM $_hsync_srv_bnds WHERE ID = ? AND Aktivno = 1"); // TRAŽI DALI JE IMA
        $_hsync_statment->bind_param("i", $_hsync_id);
        $_hsync_statment->execute();
        $_hsync_rezultat = $_hsync_statment->get_result();

        if($_hsync_rezultat->num_rows > 0) // IMA ZABRANU
        {
            session_unset();
            session_destroy();
            setcookie("_hsync_prijavljen", null, -1, "/");
            setcookie("_hsync_zabrana", $_hsync_id, time() + 31536000, "/");

            $_hsync_pristup_info = array(
                '_hsync_pristup' => -4
            );

            $_hsync_statment->close();
            $_hsync_rezultat->close();
            $_hsync_konekcija->close();
            return ($_hsync_pristup_info);
        }
    }

    $_hsync_datum = date("d. m. Y.");
    $_hsync_vrijeme = date("H:i:s");

    $_hsync_statment = $_hsync_konekcija->prepare("UPDATE $_hsync_usr SET DatumhSync = ?, VrijemehSync = ? WHERE ID = ?");
    $_hsync_statment->bind_param("ssi", $_hsync_datum, $_hsync_vrijeme, $_hsync_id);
    $_hsync_statment->execute();

    $_hsync_pristup_info = array(
        '_hsync_pristup' => 1,
        '_hsync_ime' => $_hsync_ime,
        '_hsync_id' => $_hsync_id,
        '_hsync_skin' => $_hsync_skin
    );

    return ($_hsync_pristup_info);
}

?>

Other file

require('_hsync_scripts/_hsync_pristup.php'); // FRIST LINE ON THE TOP OF FILE

I don't Know why PHP says that connection variable doesn't exist. require('_hsync_scripts/_hsync_baza.php'); is on the top, so it's global, right?

7
  • Show us your other file. Commented May 27, 2016 at 15:48
  • 1
    It's a variable scope issue. If you want to use $_hsync_konekcija inside your function, you need to declare it inside the function, pass it in as a parameter, or declare it as a global function. You have to explicitly declare global variables inside a function to be able to use them - simply having it at the top of the file doesn't affect anything Commented May 27, 2016 at 15:50
  • @AndrewCheong Other file is index.php. Function _hsync_pristup() checks if is user logged in. I'm newbie in PHP, but I think that other file won't help, because error is in _hsync_pristup.php Commented May 27, 2016 at 15:51
  • Looks like "$_hsync_konekcija->close();" is called but a connect was never called. (Line 18) Commented May 27, 2016 at 15:51
  • 2
    @SilvioCro - you'll have to ask the designers of PHP and PAWN. This is how global variables work in PHP. Commented May 27, 2016 at 15:53

2 Answers 2

2

If _hsync_konekcija is global, then tell the function that! Prior to accessing it in the function, put

global $_hsync_konekcija;

Have a look at http://php.net/manual/en/language.variables.scope.php for a fuller description of variable scope.

Sign up to request clarification or add additional context in comments.

6 Comments

global $_hsync_konekcija = new mysqli(...); PHP says Parse error: syntax error, unexpected '=', expecting ',' or ';'
@SilvioCro That's not what the answer says.
@JonStirling Then I didn't understand You. How I'll connect to base then?
@SilvioCro Re-read the answer, read the documentation that was linked to. All the information is there.
@JonStirling I use object-oriented PHP. Any way I tried did nothing. Still same error and problem.
|
1

I assume this is a scoping issue. Basically your function cant access local variables (the one you include from the file) that are instantiated outside of it.

Two options really.

1) Require the '_hsync_baza.php' file within your function.

<?php
function _hsync_pristup(){
    session_start();
    require('_hsync_scripts/_hsync_baza.php');
    ...
}

2) 'use' the variables you need.

<?php
require('_hsync_scripts/_hsync_baza.php');

function _hsync_pristup() use ($_hsync_konekcija, $_hsync_SOMETHING_ELSE, ..){
   ...
}

As a final recomendation i would remove the session_start() call from within your function to the top of the file. $_SESSION variables are 'SUPER GLOBALS' so despite being initialised from outside of the function they can be accessed from within it.

Hope this helps

1 Comment

I like frist option, but then I'll need to connect more than one time to base. Frist cunnection will be in _hsync_pristup() and second will be later in index.php. Why connect two times when you can connect one time? Also I tried with this in index.php require('_hsync_scripts/_hsync_baza.php'); require('_hsync_scripts/_hsync_pristup.php'); Include frist base, then file with function. I tought that will work.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.