Home > Developing, General, PHP, Programming > MySQL Class

MySQL Class

August 30th, 2009

In the way of changing of style of coding to OOP , I tried to wrote various classes and call them back whenever I want , as a efficient one you would feel comfortable with that is MySQL class . in old way the query had to frequently be assigned to a variable and then the mysql_query() , mysql-fetch-array() , mysql-num-rows() or etc … appeared by default in most cases .
you might choose the intricate code such as this or this , in another way you can rarely write your own class which I’ve already done . make up a simple configuration file , as instance :

1
2
3
4
5
6
<?php
$config['username'] = 'root';
$config['password'] = '';
$config['hostname'] = 'localhost';
$config['database'] = 'iportal';
?>

The $conig contains requirements to connect to database , the class I’ve written :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
<?php
require_once('config.inc.php');
 
class MySQL 
{
        var $connection;
        var $username;
        var $password;
        var $database;
        var $hostname;
 
        function setparam() 
        {
                global $config;
                $this -> hostname = $config['hostname'];
                $this -> username = $config['username'];
                $this -> password = $config['password'];
                $this -> databas  = $config['database'];
        }
        function Cmysql() 
        {
                $this -> connection = @mysql_connect( $this -> hostname , $this -> username , $this -> password ) or die("Error !");
                @mysql_select_db ( $this -> databas , $this -> connection ) or die("Error !");
        }
 
        function Dmysql() 
        {
                @mysql_close( $this -> connection );
        }
 
        function ping() 
        {
                if ( !mysql_ping ( $this -> connection ) ) {
                        $this -> Dmysql();
                        $this -> Cmysql();
                }
        }
 
        function query( $query ) 
        {
                $result = mysql_query( $query , $this -> connection ) or die (mysql_query());
                return $result;
        }
 
        function row_count ( $query ) 
        {
                $result = @$this -> query ( $query );
                $row_count = mysql_num_rows( $result );
                @mysql_free_result( $result );
                return $row_count;
        }
 
        function get_record( $query ) 
        {
                $result = $this -> query ( $query );
                $return = @mysql_fetch_assoc( $result );
                @mysql_free_result($result);
                return $return;
        }
 
        function get_records( $query ) 
        {
 
        $result = $this -> query( $query );
 
                while ( $row[] = @mysql_fetch_assoc( $result ) ) {
                        $return = $row;
        }
        @mysql_free_result($result);
                return $return;
        }
 
 
}
 
?>

Ok , we have some functions here , I describe per function exactly and shortly as they are ,
The setparam() function , sets up the variables ( you must call it before each operation ) :

$connection = new MySQL();
$connection -> setparam();

the Cmysql() and Dmysql() functions are responsible of connecting and disconnecting to database , Cmysql() must call either . they must be loaded after setparam() function .

$connection -> Cmysql ();

ping() function checks whether or not the connection to the server is working . If it has gone down , an automatic reconnection is attempted .

$connection -> ping();

query( $query ) function accepts argument and it’s MySQL query .

$connection -> query('select * from blahblah');

row_count( $query ) function counts the row(s) of a MySQL result produced by query . in old way you needed to use two functions ( I mentioned above ) while you have more maintainability with this . note it returns the row count !

$connection -> row_count ( "SELECT attempts FROM table WHERE attempts >= '5' " )

get_record( $query ) function gets the record of just one specific row by query given . in usual the query used must have WHERE clause .

$result = $connection -> get_record('select * from username where id = '1' ');

get_records( $query ) function performs the same thing with a little difference that it returns all rows as associative array .

$row = $connection -> get_records('select * from authors');

result :

Array
(
    [0] => Array
        (
            [id] => 1
            [username] => admin
            [password] => password
            [email] => wr0ng.syst3m@gmail.com
        )

    [1] => Array
        (
            [id] => 2
            [username] => yasho
            [password] => personal
            [email] => mail@gmail.com
        )

)

Here we done , include the class and call the functions , Yashar .

Developing, General, PHP, Programming , ,

  1. August 30th, 2009 at 20:03 | #1

    good work !
    nice
    ty mr yashar !

  1. No trackbacks yet.