This is the first php class i am going to share with you guys ^^
So here is the code with an explanation:
First create a .php file with your settings:
settings.php
<?php
$database = 'database_name';
$user = 'username';
$pass = 'password';
?>
Then lets create the class ^^
db.class.php
<?php
class db{
//The construct function is a function that is executed when the class is set. So here, we create the connection_id
public function __construct()
{
include_once("settings.php");
$this->connection = @mysql_connect('localhost', $user, $pass);
unset($user);
unset($pass);
mysql_select_db($database,$this->connection);
unset($database);
}
//This is the function to make querys(show lower how to)
function query($sql)
{
$result = @mysql_query($sql,$this->connection);
if($result) return $result;
else return false;
}
//This function get's the last id inserted of your connection_id (let's say you have just inserted a comment in a picture and want the id,
// use this funtion to get it!
public function last_id()
{
return @mysql_insert_id($this->connection);
}
// This function get's the number of rows of the result passed
function rows($result)
{
require("php/inc/db.php");
return @mysql_num_rows($result);
}
}
$db = new db();
?>
Now here is an example page:
example.php
<?php
require("db.class.php");
// Now lets make some queriers:
$sql ="SELECT taste FROM fruits WHERE name != 'banana'";
$result = $db->query($sql);
$total_fruits_found = $db->rows($result);
$sql = "INSERT INTO fruits(name,taste,color) VALUES ('apple','good','red')";
$db->query($sql);
$last_id = $db->last_id();
?>