Publishing System Settings Logout Login Register
PHP classes-> MySql database class
TutorialCommentsThe AuthorReport Tutorial
Tutorial Avatar
Rating
Add to Favorites
Posted on March 14th, 2008
1158 views
PHP Coding
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();
?>
 
Dig this tutorial?
Thank the author by sending him a few P2L credits!

Send
communiti.ch

Hey, I am a junior php coder for the goverment in switzerland ^^.
On my free time I usally update my website or share my knowledge with p2l =D
View Full Profile Add as Friend Send PM
Pixel2Life Home Advanced Search Search Tutorial Index Publish Tutorials Community Forums Web Hosting P2L On Facebook P2L On Twitter P2L Feeds Tutorial Index Publish Tutorials Community Forums Web Hosting P2L On Facebook P2L On Twitter P2L Feeds Pixel2life Homepage Submit a Tutorial Publish a Tutorial Join our Forums P2L Marketplace Advertise on P2L P2L Website Hosting Help and FAQ Topsites Link Exchange P2L RSS Feeds P2L Sitemap Contact Us Privacy Statement Legal P2L Facebook Fanpage Follow us on Twitter P2L Studios Portal P2L Website Hosting Back to Top