Hi all

I'm learning OOP and I'm having a bit of trouble grasping the concepts. I'm trying to code a class that will connect to a database and have a function that will insert data into a table no matter how complex the insert is. For instance:

Insert 1:
INSERT INTO animals (animal type, size, age) VALUES ($animal_type, $size, $age)

Insert 2:
INSERT INTO food (type, calories) VALUES ($type, $calories)

Basically I'm not sure how to create a function to do this or if I should make an abstract class or create an interface.

P.S. I have more than one table each with differing amounts of columns.

Hi there! I am not sure about the idea of an abstract class or an interface. Here is a simple example of a class with 3 functions, getConnection, closeConnection and insertToTable. getConnection is to connect to the database and closeConnection to close(!). The function insertToTable takes 1 parameter, that is an array. You can pass as many values you want through the array and insert relative values to the table.

<?php
class Database {
	private $db_connection;
	public function getConnection(){
		$this->db_connection=mysql_connect("localhost","root","passw");
		mysql_select_db("databasename");
		if(!$this->db_connection) {
			die('Could not connect'.mysql_error());
		} else {
			return $this->db_connection;
		} 
	}
	public function closeConnection() {
		mysql_close($this->db_connection);
	}
	public function insertToTable($data){
		 $animal_type = $data['animal_type'];
		 $animal_age = $data['animal_age'];
		 /* and all the other data */
		 $insert_to_table1 = "insert into animal (type) values ('$animal_type');
		 mysql_query($insert_to_table1);
		 $insert_to_table2 = "insert into animal_age (age) values ('$animal_age');
		 mysql_query($insert_to_table2);
	}
}	
$obj_Connection = new Database();
$obj_Connection->getConnection();
$data = array();
$data['animal_type'] = "dog";
$data['animal_age'] = 12;
$obj_Connection->insertToTable($data);
$obj_Connection->closeConnection();
?>

Cheers!

Hi there! I am not sure about the idea of an abstract class or an interface. Here is a simple example of a class with 3 functions, getConnection, closeConnection and insertToTable. getConnection is to connect to the database and closeConnection to close(!). The function insertToTable takes 1 parameter, that is an array. You can pass as many values you want through the array and insert relative values to the table.

<?php
class Database {
	private $db_connection;
	public function getConnection(){
		$this->db_connection=mysql_connect("localhost","root","passw");
		mysql_select_db("databasename");
		if(!$this->db_connection) {
			die('Could not connect'.mysql_error());
		} else {
			return $this->db_connection;
		} 
	}
	public function closeConnection() {
		mysql_close($this->db_connection);
	}
	public function insertToTable($data){
		 $animal_type = $data['animal_type'];
		 $animal_age = $data['animal_age'];
		 /* and all the other data */
		 $insert_to_table1 = "insert into animal (type) values ('$animal_type');
		 mysql_query($insert_to_table1);
		 $insert_to_table2 = "insert into animal_age (age) values ('$animal_age');
		 mysql_query($insert_to_table2);
	}
}	
$obj_Connection = new Database();
$obj_Connection->getConnection();
$data = array();
$data['animal_type'] = "dog";
$data['animal_age'] = 12;
$obj_Connection->insertToTable($data);
$obj_Connection->closeConnection();
?>

Cheers!

Thanks for your response nav33n. The trouble comes in when I have multiple tables. One for food, animals, mechanical etc. Just want to create a something that will be able to work across the website.

The example posted above can also deal with multiple tables. But, if you have different forms and different operations (like one form inserting a record to food table, another form to animals table, etc), you can have multiple functions. Sorry for my ignorance, but, I am not really sure what exactly is the problem :-/

Would something like this work?

public function insertToTable($table, $columns, $data){
		 $insert_to_table = "insert into $table ($columns) values ('$data')";
		 mysql_query($insert_to_table);
}

Code that goes on the animals page

$obj_Connection = new Database();
$obj_Connection->getConnection();
$table = "animals";
$columns = "type, age, length";
$data = "dog, 5, 1.5m";
$obj_Connection->insertToTable($table, $columns, $data);
$obj_Connection->closeConnection();

Code that goes on the food page

$obj_Connection = new Database();
$obj_Connection->getConnection();
$table = "food";
$columns = "type, calories";
$data = "cake, 1000";
$obj_Connection->insertToTable($table, $columns, $data);
$obj_Connection->closeConnection();

Umm.. Yeah, well, almost.. This looks good except the part where you insert string values to a varchar column of the table. Strings should be wrapped in ' single quotes.. So, pass the values accordingly.. ie.,

$obj_Connection = new Database();
$obj_Connection->getConnection();
$table = "food";
$columns = "type, calories";
$data = "[single_quote_here]'cake'[/single_quote], 1000";
$obj_Connection->insertToTable($table, $columns, $data);
$obj_Connection->closeConnection();

I hope you understood what I am talking bout.

Comments
Thanks for the help. Really appreciate it ;)
This question has already been answered. Start a new discussion instead.