1

I have a problem. When I use global variables in my php code, my server doesn't show any html code at all. If I comment out the global variable, my html page works just fine! Am I doing something wrong here?

php file:

class DBConnect{

    // If I comment this out, the HTML shows
    global $con;

    function connectDB() {
        $user = "bstokni_basurin";
        $pass = "basurin";
        $database = "basurin";
        $host = "localhost";

        $this->$con = mysql_connect($host, $user, $pass) or die(mysql_error());
        echo "Connected to MySQL </br>";
        echo $con;
    }

    function closeDB() {
        mysql_close($con);
        echo $con;
        echo "MySQL closed";
    }   
}

html file:

<!-- Left colon -->
            <div id="leftCol">
                <p>Her kemur ein menu at standa</p>
                <?
                    $menuObj = new DBConnect();
                    $menuObj->connectDB();
                ?>
            </div>

What am I doing wrong here?

2
  • 1
    You're getting a PHP error but the errors are suppressed. Put this at the top of the page and tell us what you get error_reporting(E_ALL); ini_set('display_errors', '1'); Commented Jun 25, 2011 at 2:36
  • @Mike Swift - It could also be that styles are just hiding the error or warning. @hogni89, have you tried selecting the text in the vicinity of the problematic div, or tried installing custom error handlers? Commented Jun 25, 2011 at 2:38

2 Answers 2

3

Since the variable is in a class scope, try changing global to public instead. You don't seem to need global in the example you have provided.

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

3 Comments

And it's probably not expected, meaning it's (as Mike Swift points out) throwing a suppressed error. Here's a proof: codepad.org/b5JsmgPx
Thanks for your answer. I'm new to class in PHP. I had two problems. First of all: $this->$con = ... should be: $this->con = ... And changing from global to public also worked :) I can go to sleep now ;)
Add function __construct() { global $test; } and change global $test; in your current project to public $text;. It will set the variable as global when the class is instantiated.
3

Shouldn't:

$this->$con

be:

$this->con

?

If you're just trying to access a member variable named con. Everywhere else you refer to is by just $con, why the $this in the one place? I'm not sure what $this->$con is supposed to do, but I'm guessing it is affected by whether or not $con has been declared global.

4 Comments

$this->$con will try to access the public variable named whatever the value of $con is. See this example. In his case, $this->$con is some MySQL resource that will probably be wrongly interpreted as a string.
Ya the extra $ will blow it up every time. But you need to checking your errors too, so do as suggested by mike swift as well.
I had a feeling. But the contents of $con would be null in both cases, wouldn't they? So $this->$con = ... should be assigning to the same (wrong) place whether there is a global $con declaration or not. So how does one case work and the other not?
global isn't correct to use as a class variable (I misspoke earlier, it doesn't matter that it's a public or private variable in this case, just a class variable), that's what the error was. In my case, I'm only assigning one class variable; it only prints the one it knows about.

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.