1 Replies - 25830 Views - Last Post: 27 January 2010 - 05:51 AM Rate Topic: -----

#1 xoops  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 81
  • Joined: 06-August 09

example create, read, update, delete (CRUD) using OOP in PHP

Posted 27 January 2010 - 01:43 AM

Hello,
I want question how i can find example create, read, update, delete (CRUD) to MySQL using OOP in PHP. Thank's
Is This A Good Question/Topic? 0
  • +

Replies To: example create, read, update, delete (CRUD) using OOP in PHP

#2 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2891
  • View blog
  • Posts: 10,025
  • Joined: 08-August 08

Re: example create, read, update, delete (CRUD) using OOP in PHP

Posted 27 January 2010 - 05:51 AM

Here's a class I've used as a starting point. It can read/update multiple tables.
<?php 
class general {
	var	$id;
	var	$row;
	var	$table = array("some","tables");
	var	$dblink;
	var	$servername='localhost';
	var	$dbusername='root';
	var	$dbpassword='root';
	var	$dbname='somdatabase';

function __construct() {
	$this->dblink = mysql_connect ($this->servername, $this->dbusername,$this->dbpassword) or die('CONNECTION ERROR: Could not connect to MySQL');
	mysql_select_db($this->dbname,$this->dblink) or die ('Could not open database '.mysql_error());
	}

function read_table($x) {
	$query = "SELECT * FROM ".$this->table[$x]." where id = '".$this->id."'";
	$dbdata = mysql_query($query,$this->dblink) or die('Error reading from '.$this->table[$x]." ".mysql_error());
	$this->row = mysql_fetch_array($dbdata);
	}
	
function update_table($x) {
	$query = "update ".$this->table[$x]." set ";
	foreach($this->row as $key => $value) {
		if(!is_numeric($key) && $key != "id") { 
			$query .= $key." = '".$value."', ";
			}
		}
	$query = substr($query,0,-2)." where id ='".$this->id."'";
	mysql_query($query,$this->dblink) or die("Error updating ".$this->table[$x]." ".mysql_error());
	}

}	
?>


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1