8bdcb8b82adf1dd562970b22d3458d7d

Hello ppl i'm new to php\\OOP and i'm trying to build a simple wrapper.
Any adice refactoring would be nice
Thank you.

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
77
78
79
80
81
82
83
<?php

if (!defined('ANTIHACK'))
{
    header('HTTP/1.0 403 Forbidden');
    header('Status: 403 Forbidden');
    die ("<br> You don't have access here.");
}
Class MySQL
{

    var $dbConfigInfo;
    var $connect;
    var $row;

    public static function connect($dbConfigInfo = NULL)
    {
        static $connect;

        if (!is_array($dbConfigInfo) || count($dbConfigInfo) < 1)

        return FALSE;
        if (! isset ($connect))
        {
            $connect = mysql_connect($dbConfigInfo['servername'], $dbConfigInfo['username'], $dbConfigInfo['password']);

            return $connect;
        }
    }
	
	public static function db($dbname)
	{
		$db = mysql_select_db($dbname) or die(mysql_error());
		
			return $db;
		
	}
	
	public static function query($query)
	{
		return mysql_query($query, $connect);
	}
	
	public static function FetchArray($result = '')
	{
		return $row = mysql_fetch_array($result, MYSQL_BOTH);
	}
	
}
?>


and this is how i use it


<?php 


	$dbConfigInfo = array(
	   'servername' => 'localhost',
	   'username'   => 'root',
	   'password'   => '',
	   'dbname'     => 'gallery', 
	);
	
	define('TBL_PRODUCTS', 'sometable');
	define('TBL_CATEGORYS', 'sometable');
	
	DEBUG ? error_reporting(E_ALL | E_STRICT) : error_reporting(0);
	
	if(!class_exists('MySQL'))
		include_once('classes/mysql.class.php');
	$connect = MySQL::connect($dbConfigInfo);
	
	if(!$connect)
		die("Nu m-am putut conecta"); // TODO : Modificare erroare handler errori custom
	$db_select = MySQL::db($dbConfigInfo['dbname']);
	
	if(!$db_select)
		die("Nu am ptutu selecta."); // TODO : Modificare erroare handler errori custom
		

?>

Refactorings

No refactoring yet !

441c4f02db55ef2cbe96027af7012e01

Tim Cooper

April 3, 2009, April 03, 2009 19:26, permalink

No rating. Login to rate!

Just a note -- PHP already has a great database wrapper which I highly recommend you use: http://php.net/PDO

B52190dddc9e041469cfd181cb30941d

Byron

April 3, 2009, April 03, 2009 21:01, permalink

No rating. Login to rate!

Hello. That's very nice site but I've seen this before here http://text.inguaro.com/e3a8467a8005187b01a737aba0830b81
e3a8467a8005187b01a737aba0830b81

1
2
Hello. That's very nice site but I've seen this before here http://text.inguaro.com/e3a8467a8005187b01a737aba0830b81
e3a8467a8005187b01a737aba0830b81
8bdcb8b82adf1dd562970b22d3458d7d

fantomel

April 4, 2009, April 04, 2009 08:37, permalink

No rating. Login to rate!

i know about PDO and mysqli and i now that some of you consider writing a class for mysql it's a waste of time when instead of using mysql i can be used mysqli or PDO.
The scope of the class is for learning purpose:)

Avatar

Alix Axel

June 16, 2009, June 16, 2009 00:58, permalink

No rating. Login to rate!

my 0.02€

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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
<?php

class MySQL
{
	public $db = null;

	function __construct($database, $username, $password, $hostname = 'localhost', $port = 3306)
	{
		$this->db = mysql_connect($hostname . ':' . intval($port), $username, $password);

		if (is_resource($this->db) === true)
		{
			if (mysql_select_db($database, $this->db) === true)
			{
				$this->Query('SET NAMES ' . $this->Quote('utf8') . ';');
				$this->Query('SET CHARACTER SET ' . $this->Quote('utf8') . ';');
			}
		}
	}

	function __destruct()
	{
		if (is_resource($this->db) === true)
		{
			mysql_close($this->db);
		}
	}

	function Query($sql, $type = 'assoc')
	{
		if (is_resource($this->db) === true)
		{
			$query = mysql_query($sql, $this->db);

			if (is_resource($query) === true)
			{
				$result = array();

				for ($i = 0; $i < mysql_num_rows($query); $i++)
				{
					$result[$i] = call_user_func('mysql_fetch_' . $type, $query);
				}

				mysql_free_result($query);

				return $result;
			}

			else if ($query === true)
			{
				switch (strtoupper(substr($sql, 0, strpos($sql, ' '))))
				{
					case 'INSERT':
						return mysql_insert_id($this->db);
					break;

					case 'UPDATE':
					case 'DELETE':
						return mysql_affected_rows($this->db);
					break;
				}

				return true;
			}
		}

		return false;
	}

	function Quote($string)
	{
		if (get_magic_quotes_gpc() === true)
		{
			$string = stripslashes($string);
		}

		return '\'' . mysql_real_escape_string(trim($string), $this->db) . '\'';
	}
}

$db = new MySQL('test', 'root', '');

var_dump($db->Query('INSERT ...'));
var_dump($db->Query('DELETE ...'));
var_dump($db->Query('UPDATE ...'));

echo '<pre>';
print_r($db->Query('SELECT ...'));
echo '</pre>';

?>
D9a353bffb7f54d2330c86d42203032d

murugan

September 23, 2009, September 23, 2009 06:29, permalink

No rating. Login to rate!

test

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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
<?php

class MySQL
{
	public $db = null;

	function __construct($database, $username, $password, $hostname = 'localhost', $port = 3306)
	{
		$this->db = mysql_connect($hostname . ':' . intval($port), $username, $password);

		if (is_resource($this->db) === true)
		{
			if (mysql_select_db($database, $this->db) === true)
			{
				$this->Query('SET NAMES ' . $this->Quote('utf8') . ';');
				$this->Query('SET CHARACTER SET ' . $this->Quote('utf8') . ';');
			}
		}
	}

	function __destruct()
	{
		if (is_resource($this->db) === true)
		{
			mysql_close($this->db);
		}
	}

	function Query($sql, $type = 'assoc')
	{
		if (is_resource($this->db) === true)
		{
			$query = mysql_query($sql, $this->db);

			if (is_resource($query) === true)
			{
				$result = array();

				for ($i = 0; $i < mysql_num_rows($query); $i++)
				{
					$result[$i] = call_user_func('mysql_fetch_' . $type, $query);
				}

				mysql_free_result($query);

				return $result;
			}

			else if ($query === true)
			{
				switch (strtoupper(substr($sql, 0, strpos($sql, ' '))))
				{
					case 'INSERT':
						return mysql_insert_id($this->db);
					break;

					case 'UPDATE':
					case 'DELETE':
						return mysql_affected_rows($this->db);
					break;
				}

				return true;
			}
		}

		return false;
	}

	function Quote($string)
	{
		if (get_magic_quotes_gpc() === true)
		{
			$string = stripslashes($string);
		}

		return '\'' . mysql_real_escape_string(trim($string), $this->db) . '\'';
	}
}

$db = new MySQL('test', 'root', '');

var_dump($db->Query('INSERT ...'));
var_dump($db->Query('DELETE ...'));
var_dump($db->Query('UPDATE ...'));

echo '<pre>';
print_r($db->Query('SELECT ...'));
echo '</pre>';

?>

Your refactoring





Format Copy from initial code

or Cancel