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)

<?php
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'])';
if(mysql_query($query){
echo "Successful Inserted!";
header('Location:/site/view.php');
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;

}

}

?>

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.

do you mean this?

constant var = 5;

I have seen it in manual but not used it!

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)

modified but I still get error!
I cannot understand (do I need to rest?)
Uuuh!

Any reason why you are wanting to set it up that way?

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.

thanks!

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.

Any suggestion on reformatting the above code?
Is it good enough to do the job?
Thanks for the answer!

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 :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.