Home of the Ultimate Xbox 360 Modding Tool, Horizon. XboxMB.com is a community of Xbox 360 gamers and modders who share Tutorials, News, Reviews, and other resources. Xbox Message Boards is free to sign up and use, so what are you waiting for? Register Now!
Well I see a lot of people who don't use Object Oriented PHP so this tutorial isn't really a proper tutorial but more of a insight into OOP. Basically OOP is the future in my opinion but it hasn't always been there. It was introduced in PHP5 and has rapidly grown.
/**
* __construct()
* Sets the user details when class is initilized
*
* @param string $username
* @param string $email
* @param string $firstName
* @param string $lastName
*/
function __construct($username, $email, $firstName, $lastName)
{
// Seting the private variables
$this->username = $username;
$this->email = $email;
$this->firstName = $firstName;
$this->lastName = $lastName;
}
/**
* user/returnUserDetails
* function will pull user info from our private vars and return them.
*
* @return array
*/
function returnUserDetails()
{
// Stores user details from the private vars in an array
$details = array(
'username' => $this->username,
'email' => $this->email,
'firstName' => $this->firstName,
'lastName' => $this->lastName
);
// Returns the array with all the user details
return $details;
}
}
?>
That is a very simple class for storing user details and accessing them. Now time to show you how to use the class.
PHP Code:
// Initilizes the 'user' class and stores the instance in this variable
$userClass = new user('John', 'example@hotmail.com', 'John', 'Smith');
// Stores the user details which was returned from the 'returnUserDtails' function in the 'user' class
$user = $userClass->returnUserDetails();
// Prints the array onto the document
echo '<pre/>';
print_r($user);
Here is how you print one object of the array like username for example.
PHP Code:
// Initilizes the 'user' class and stores the instance in this variable
$userClass = new user('John', 'example@hotmail.com', 'John', 'Smith');
// Stores the user details which was returned from the 'returnUserDtails' function in the 'user' class
$user = $userClass->returnUserDetails();
echo $user['username'];
This may seem over complicated over something so small but when you have a large project this saves so much time, easier to read and just generally is a lot better than using the old procedural methods like this for example
PHP Code:
<?php
/*
* File Name: Database.php
* Date: November 18, 2008
* Author: Angelo Rodrigues
* Description: Contains database connection, result
* Management functions, input validation
*
* All functions return true if completed
* successfully and false if an error
* occurred
*
*/
class Database
{
private $con = false; // Checks to see if the connection is active
private $result = array(); // Results that are returned from the query
/*
* Connects to the database, only one connection
* allowed
*/
public function connect()
{
if(!$this->con)
{
$myconn = @mysql_connect($this->db_host,$this->db_user,$this->db_pass);
if($myconn)
{
$seldb = @mysql_select_db($this->db_name,$myconn);
if($seldb)
{
$this->con = true;
return true;
}
else
{
return false;
}
}
else
{
return false;
}
}
else
{
return true;
}
}
/*
* Changes the new database, sets all current results
* to null
*/
public function setDatabase($name)
{
if($this->con)
{
if(@mysql_close())
{
$this->con = false;
$this->results = null;
$this->db_name = $name;
$this->connect();
}
}
}
/*
* Checks to see if the table exists when performing
* queries
*/
private function tableExists($table)
{
$tablesInDb = @mysql_query('SHOW TABLES FROM '.$this->db_name.' LIKE "'.$table.'"');
if($tablesInDb)
{
if(mysql_num_rows($tablesInDb)==1)
{
return true;
}
else
{
return false;
}
}
}
/*
* Selects information from the database.
* Required: table (the name of the table)
* Optional: rows (the columns requested, separated by commas)
* where (column = value as a string)
* order (column DIRECTION as a string)
*/
public function select($table, $rows = '*', $where = null, $order = null)
{
$q = 'SELECT '.$rows.' FROM '.$table;
if($where != null)
$q .= ' WHERE '.$where;
if($order != null)
$q .= ' ORDER BY '.$order;
/*
* Insert values into the table
* Required: table (the name of the table)
* values (the values to be inserted)
* Optional: rows (if values don't match the number of rows)
*/
public function insert($table,$values,$rows = null)
{
if($this->tableExists($table))
{
$insert = 'INSERT INTO '.$table;
if($rows != null)
{
$insert .= ' ('.$rows.')';
}
/*
* Deletes table or records where condition is true
* Required: table (the name of the table)
* Optional: where (condition [column = value])
*/
public function delete($table,$where = null)
{
if($this->tableExists($table))
{
if($where == null)
{
$delete = 'DELETE '.$table;
}
else
{
$delete = 'DELETE FROM '.$table.' WHERE '.$where;
}
$del = @mysql_query($delete);
/*
* Updates the database with the values sent
* Required: table (the name of the table to be updated
* rows (the rows/values in a key/value array
* where (the row/condition in an array (row,condition) )
*/
public function update($table,$rows,$where)
{
if($this->tableExists($table))
{
// Parse the where values
// even values (including 0) contain the where rows
// odd values contain the clauses for the row
for($i = 0; $i < count($where); $i++)
{
if($i%2 != 0)
{
if(is_string($where[$i]))
{
if(($i+1) != null)
$where[$i] = '"'.$where[$i].'" AND ';
else
$where[$i] = '"'.$where[$i].'"';
}
}
}
$where = implode('',$where);
Programming today is a race between software engineers striving to build bigger and better
idiot-proof programs, and the Universe trying to produce bigger and better idiots.
So far, the Universe is winning.
Now what if I want to actually fetch the user details from a Database?
Care to make tutorial on that?
Sure. Would help a lot of new people to PHP as I have noticed that they like to create login scripts and stuff like that which requires database interaction.