Jump to content

Recommended Posts

I'm new to PHP OOP. Currently I'm trying to make some simple classes to understand how they work in real life applications and maybe create something with these testing classes I'm trying to make. Right now I'm stuck with first class and have no idea how do solve the problems.

 

Class I created is simple and should make connection to database and disconnect. It works - half of it works. It makes connection to database (I can retrieve records) but I think disconnecting part doesn't work as it should. Code I have so far:

<?php

class db_connect
{
    // declare hostname, username, password and db name
    private $host, $user, $pass, $db_name;
    
    // set variables
    function __construct($host, $user, $pass, $db_name)
    {
        $this->host = $host;
        $this->user = $user;
        $this->pass = $pass;
        $this->db_name = $db_name;
    }
    
    // makes db connection. returns true if there is already connection made else tries to make it
    function connect()
    {
        if(!$this->con)
        {
            // if no connection ($con is false) make one
            $c = mysql_connect($this->host, $this->user, $this->pass);
            $s = mysql_select_db($this->db_name, $c);
            // check if connection and db is ok
            if($c && $s)
            {
                // they are. $con true
                $this->con = true;
                return true;
            }
            else
            {
                // they are not fine. returns false
                return false;
            }
        }
        else
        {
            return false;
        }
    }
    
    function disconnect()
    {
        // if there is connection then disconnect
        if($this->con)
        {
            // close connection
            $dis = mysql_close();
            // make $con false
            $this->con = false;
            //return if clause true
            return true;
        }
    }
}

?>

<?php

include("class.db_connect.php");
$db = new db_connect("localhost", "root", "", "funhouse");
$db->connect();
$sql = mysql_query("SELECT * FROM db LIMIT 1, 30") or die(mysql_error());

while($row = mysql_fetch_assoc($sql))
{
    echo $row['name'] . "<br>";
}
#$db->disconnect();

?>

 

As you can see, I've commented out disconnecting part. This is because when this line is uncommented, I get

This webpage is not available.

The webpage at http://www.localhost/dev/test.php/ might be temporarily down or it may have moved permanently to a new web address.

I also get notice:

Notice: Undefined property: db_connect::$con in C:\wamp\www\dev\class.db_connect.php on line 25

It's this line: if(!$this->con).

So my question is - What am I doing wrong? Why do I get 'The webpage is not available' and how do fix this notice?

Like the error states, your object doesn't have a property named $con.  You need to declare it with the rest of your database properties (like you did with $host, $pass, etc.) before you can use it.

Ok. I declared this like all others. Now the notice is gone. But still, disconnecting part doesn't work. It loads quite long and then displays same stuff - 'This webpage is not available.'. Any idea how do fix this..?

 

Oh, and also, I should close db connection at bottom of every page, right?

Like the error states, your object doesn't have a property named $con.  You need to declare it with the rest of your database properties (like you did with $host, $pass, etc.) before you can use it.

Ok. I declared this like all others. Now the notice is gone. But still, disconnecting part doesn't work. It loads quite long and then displays same stuff - 'This webpage is not available.'. Any idea how do fix this..?

 

Oh, and also, I should close db connection at bottom of every page, right?

 

Do a test - do you get the same error when not using your object to handle the db connection?

Like the error states, your object doesn't have a property named $con.  You need to declare it with the rest of your database properties (like you did with $host, $pass, etc.) before you can use it.

Ok. I declared this like all others. Now the notice is gone. But still, disconnecting part doesn't work. It loads quite long and then displays same stuff - 'This webpage is not available.'. Any idea how do fix this..?

 

Oh, and also, I should close db connection at bottom of every page, right?

 

Do a test - do you get the same error when not using your object to handle the db connection?

Wow.. mysql_close() wanted the variable I assigned to mysql_connect().. Now it works.

Thank you!!!

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.