Help me inspect this code.
I cannot see error by myself.
here are the codes for inc.database.php

I'm learning OOP with PHP (I extensively using it with python but I'm noob to PHPiing)

class Connectdb{
private	$DATABASE = "site_contents";
private	$HOST = "localhost";
private	$USER = "root";
private	$PASS = "jesus";

public function __construct(){}

public function connect($host = $HOST, $user = $USER = "root", $passwd = $PASS, $db = $DATABASE ){
$conn = mysql_connect($host, $user, $passwd) or die("Cannot Connect to the database $db");
mysql_select_db($db, $conn) or die("Unable to select database");
return $conn;

public function insertdata($conn, $array_values){
//array should have id, update_date , description , heading , contents, creator 
if (isset(_POST["save"])){
$query = 'INSERT INTO articles(id, date_date , description , heading , contents, creator) VALUES($array_values['id'], $array_values['update_date'], $array_values['description'], $array_values['heading'], $array_values['contents'], $array_values['creator'])';
echo "Successful Inserted!";
die("Illegal action, contact admin!");

public function retrievedata($conn, $table){
$query = 'SELECT * FROM $table';
$result = mysql_query($query);
$row = mysql_fetch_object($result);
return $row;



9 Years
Discussion Span
Last Post by Stefano Mtangoo

You access class variables with $this->var_name This is whats causing the error:

public function connect($host = $HOST, $user = $USER = "root", $passwd = $PASS, $db = $DATABASE ){

should be:

public function connect($host = $this->HOST, $user = $this->USER = "root", $passwd = $this->PASS, $db = $this->DATABASE ){

I think that should work. I have used class constants in the function before. I am not sure if you can use class variables.

If not, then change the variables to constants and use them that way.

Edited by somedude3488: n/a


Within the class you use const var = 5; .

To access it, you need to use class_name::const_name or use self in php5. self::const_name (only works inside the class the constant is defined in)


Ok all I want is having class that will handle all database issues. All I'm trying to accomplish is making very simple CMS for my little website (developing in WAMP). I have set already Editor, and now I want to setup database functions to do the inserting of articles, editing and deleting them.
you can help me get it right if I'm doing it wrongly or suggest better if not best method of doing it.



You need a database class that handles all basic functions. Don't make it specific towards anything.

After you have that then you create other classes (ect. article class) that will handle specific tables within the database. It keep things organized better and makes the code more flexible.


I found removing default value in the line

public function connect($host = $this->HOST, $user = $this->USER = "root", $passwd = $this->PASS, $db = $this->DATABASE ){

works. I'm trying to use constants

public function connect($host = self::HOST, $user = self::USER, $passwd = self::PASS, $db = self::DATABASE)

gives no error :)

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.