0

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;

}

}

?>
2
Contributors
10
Replies
11
Views
8 Years
Discussion Span
Last Post by Stefano Mtangoo
0

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

0

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)

0

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!

0

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.

0

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

0
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.