Mars MySQL Easy PHP Class v1.0

Started on the old forum in May 2010 · view the original in the archive · old links redirect here

#class #mars #mysql #php

No replies · 898 views · started 16 years ago · 1 min read

Mars Junior · 34 posts #1

Idle time in mastering and light, and simultaneously professional mysql class from me, version 1.0.0.1 beta

preview:

<?php
/*
``````````````````````````````````````````````````
``````````````````````````````````````````````````
``````````````````````````````````````````````````
``````````````````````````````````````````````````
````````````````````````````````````````S8$$$o````
``````````````````````````````````````S¶$o````````
``````````````o¶S``````````````S$$$$oo¶8``````````
```8S8$8``````8¶¶$o``````````S$¶S`o¶So¶8``````````
```o¶8o8¶S````¶8`$$``````````o$$```¶S`S¶S`````````
````S¶o`$$o```¶8`o$$``````````o¶8o8¶o``8$$$$$$$$8`
`````8¶o`8$8oo¶S``o¶$``````````S¶¶$o```````````o¶8
``````¶8```8¶$¶````o¶S``````````8¶¶S````````````8$
``````S¶o````88`````8¶````oo`````¶8¶$o`````````o$$
```````$$````````````$8``S¶$¶o```¶8o$¶o```````8¶8o
```````o¶8```````````S¶``S¶S¶$o``8$``8¶S```o8$8o``
````````S¶o```````````¶S`S¶¶8$¶o`8¶```$¶``````````
`````````$$```````````¶$`8¶S``¶$`8¶```o¶S`````````
`````````8¶```````````$$`S¶S``o$`oS```````````````
`````````8¶```````````$$`S¶S``````````````````````
`````````8¶```````````$$`o¶o``````````````````````
`````````8¶```````````8¶``````````````````````````
`````````8¶````````````o``````````````````````````
`````````oo```````````````````````````````````````
``````````````````````````````````````````````````
*/

//global constants
define ('UNBUFFERED', 'unbuffered', true);
define ('DEFAULT_SQL_TYPE', 'default', true);

# class mysql, version 1.0
class mars_sql_class 
{
	# amount request sql by default
	var $sql_count = 0;
	
	# data for connection to base
	var $sql_connect = array(
	'hostname' => 'localhost',
	'username' => 'dbuser',
	'password' => 'dbpass',
	'mydbname' => 'mydb',
	);
	
	# variable protection
	var $max_sql_requests = 250; //maximum number sql-queries
	var $db_is_connected = false; //switch does not give to create repeated join to base
	var $sql_memory_used = 0; //count spent memories (not physical)
	
	/*//-----------------------
	* --------------------------
	* VERSION 1.0.0.1 BETA
	* --------------------------
	*///-----------------------

	//function of the join with base mysql
	function connect () 
	{
		if (!$this->db_is_connected) 
		{
			MYSQL_CONNECT($this->sql_connect['hostname'], $this->sql_connect['username'], $this->sql_connect['password']) or die('MYSQL_CONNECT ^#@#%^!$#@^&W!');
			MYSQL_SELECT_DB($this->sql_connect['mydbname']) or die('MYSQL_SELECT_DB ^#@#%^!$#@^&W!');
			$this->db_is_connected = true;
			$this->sql_memory_used += (ceil((strlen($this->sql_connect['username'])+strlen($this->sql_connect['mydbname']))*M_PI));
			$this->sql_connect = null;
		}
	}
	
	//query mysql
	function query ($sql, $type = DEFAULT_SQL_TYPE)
	{
		$this->sql_count++;
		if ($this->sql_count > $this->max_sql_requests) die('^#@#%^!$#@^&W!');
		$query_function = ($type === UNBUFFERED) ? 'mysql_unbuffered_query' : 'mysql_query';
		$this->sql_memory_used += ($type === UNBUFFERED) ? (ceil(M_PI)) : (ceil(M_PI*strlen($sql)));
		return $query_function($sql);
	}
	
	//mysql_num_rows
	function sql_numrows ($result = false)
	{
		$num_rows = false;
		if ($result) $num_rows = is_resource($result) ? mysql_num_rows($result) : false;
		return $num_rows;
	}
	
	//standard fetchrow answer
	function sql_fetchrow ($result, $result_type = MYSQL_ASSOC)
	{
		return is_resource($result) ? mysql_fetch_array($result, $result_type) : false;
	}
	
	//collection of the answer in array
	function sql_fetchrowset ($result, $result_type = MYSQL_ASSOC)
	{
		$rowset = array();

		while ($row = mysql_fetch_array($result, $result_type))
		{
			$rowset[] = $row;
		}

		return $rowset;
	}
	
	//typical scaffolding, structure: N, TITLE, CONTENT; SQL operator of the apropriation use for calling 'AS': SELECT id AS N, bugagag AS TITLE, hmm AS CONTENT FROM `mytable`
	function get_entries ($result)
	{
		$entries_array = array();
		while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) 
		{
			$entries_array[] = (strlen($row['CONTENT'])>1) ? array($row['N'], $row['TITLE'], $row['CONTENT']) : array($row['N'], $row['TITLE']);
		}
		return $entries_array;
	}
	
	//query mysql with blackjack and hookers, например SELECT id AS N, title AS TITLE, MID(body, 1, 256) AS CONTENT FROM `entries` ORDER BY N DESC LIMIT 0,3
	function get_entries_sql ($sql)
	{
		return $this->get_entries($this->query($sql));
	}
	
	//cleaning result request
	function sql_freeresult ($result)
	{
		if ($result)
		{
			$tmp_ir = is_resource($result);
			$return_value = ($tmp_ir) ? mysql_free_result($result) : false;
			$this->sql_memory_used += ($tmp_ir) ? (($this->sql_memory_used < 1)?0:(-1*(ceil(M_PI*(M_LN10*10))))) : 0;
		}
		
	}

}

$db = new mars_sql_class;
?>

use variants:

require 'mars.class.mysql.en.php';

$db->sql_connect = array(
'hostname' => 'localhost',
'username' => 'dbuser',
'password' => 'dbpass',
'mydbname' => 'mydb',
); //it is not recommended to expose on page, recommended change this in class

$db->connect();

$sql = "SHOW TABLES";

$result = $db->query($sql);

echo 'Total queries: '.$db->sql_count.', mem used: '.$db->sql_memory_used;
require 'mars.class.mysql.en.php';
if ($simple_cache_var)
{
print_cached_page();
}
else
{
$db->connect();

$sql = "SHOW TABLES";

$result = $db->query($sql);
ob_start();
echo 'Total queries: '.$db->sql_count.', mem used: '.$db->sql_memory_used;
...

Sign in to reply.

Similar topics

Keyboard shortcuts

g f
Go to forums
g h
Go to your feed
g m
Go to messages
r
Reply (in a topic)
/
Search
j
Next post
k
Previous post
?
This list
Ctrl/⌘ + Enter
Send what you are writing
Ctrl/⌘ + B / I
Bold / italic while writing
Esc
Close