当前位置: 首页 > 工具软件 > chillyCMS > 使用案例 >

Investigation into chillyCMS (Day1)

盖翰池
2023-12-01


This time, I go through from down to top, for simplicity. And the most simple class should be the one responsible for database accessing, relying on none custom class.


DataBase Class


There really does be one, database.class.php:


<?php
//##############################################################################
//	Database class
//##############################################################################
//	chillyCMS - Content Management System
//##############################################################################

if (file_exists('config.php'))
{
	require_once('config.php');
}
elseif (file_exists('../config.php'))
{
	require_once('../config.php');
}
elseif (file_exists('../../config.php'))
{
	require_once('../../config.php');
}
require_once(PATH.'/core/helpers.include.php');

class Database {

	//Class variables///
	private $host;			//db server address
	private $username;		//db username
	private $password;		//db password
	public	$database;		//db name
	private $lastquery;		//last executed query
	public $connection;		//actual connection to db
	
	//Functions/
	//Constructor
	function __construct()
	{
		$this->host		= DB_HOST;
		$this->username		= DB_USER;
		$this->password		= DB_PW;
		$this->database		= DB_DB;
		$this->lastquery	= false;
		$this->connection	= false;
		$this->connect();
	}
	
	//Destructor
	function __destruct()
	{
		$this->close();
	}
	
	//connect to the database
	private function connect()
	{
		//try to connect to the db Server
		$this->connection = @mysql_connect($this->host, $this->username, $this->password);

		if($this->connection == false)
		{
			die("Cannot connect to server '".$this->host."'");
		}
		//try to select the db
		$dbconnect = @mysql_select_db($this->database, $this->connection);
		if($dbconnect == false)
		{
			die("Cannot connect to database '".$this->database."'");
		}
	}
	
	//do a query
	public function query($sql)
	{
		$return = false;
		//echo "<em>Class DB:</em> $sql<br>";
		if (is_resource($this->connection))
		{
			if ($this->lastquery = mysql_query($sql, $this->connection))
			{
				$return = true;
			}
		}
		return $return;
	}
	
	//get a whole table
	public function get_table($table,$order=1)
	{
		$table=$this->escape($table);
		$order=$this->escape($order);
		if ($this->query("select * from $table order by $order"))
		{
			return true;
		}
		else
		{
			return false;
		}
	}

	//escape a given string
	public function escape($string)
	{
		if(get_magic_quotes_gpc())
		{
			$string = stripslashes($string);
		}
		return @mysql_real_escape_string($string,$this->connection);
	}

	//get query result in a form depending on the number of returned rows
	public function getdata()
	{
		$resultCount = @mysql_num_rows($this->lastquery);
		$result = false;

		//if only one line is returned save it in a var
		if ($resultCount==1)
		{
			$result = @mysql_fetch_assoc($this->lastquery);

		}
		//if there are more lines save them in an array
		else if ($resultCount > 1)
		{
			while($row = @mysql_fetch_assoc($this->lastquery))
			{
				$result[]=$row;
			}
		}
		else
		{
			$result = true;
		}
		return $result;
	}
	
	//get query result as array
	public function getdata_array() {
		$result=array();
		while($row = @mysql_fetch_assoc($this->lastquery))
		{
			$result[]=$row;
		}
		return $result;
	}
	
	//get result count
	public function query_count()
	{
		return @mysql_num_rows($this->lastquery);
	}
	
	//find out next autoincrement value
	public function next_autoincrement($table)
	{
		$table = $this->escape($table);
		if ($this->query("show table status like '$table'"))
		{
			$result=$this->getdata();
			$result=$result["Auto_increment"];
		}
		else
		{
			$result=false;
		}
		return $result;
	}
	
	//close the database connection
	public function close()
	{
		if (is_resource($this->lastquery))
		{
			@mysql_free_result($this->lastquery);
		}
		if (is_resource($this->connection))
		{
			@mysql_close($this->connection);
		}
		$this->lastquery=$this->connection=$this->host=$this->username=$this->password=$this->database=false;
		unset($this);
	}
} 
?>

It cut up the process of fetching data from mySQL into 3 pieces: 

1. connecting to mySQL, 

2. executing a query, 

3. loading the data from return result to local array.





 

Constructor & Destructor in PHP5:


According to doc on php.net, __construct() and __destruct() are new-standard method for construction and destruction.


Shorthand Approach of Assignment to Array:


There is block in the php file:


			while($row = @mysql_fetch_assoc($this->lastquery))
			{
				$result[]=$row;
			}

I design my own test and the result goes as fine as expection:


<?php
/*
 * Created on 2011/7/26
 *
 * To change the template for this generated file go to
 * Window - Preferences - PHPeclipse - PHP - Code Templates
 */

$host = 'localhost';
$user = 'root';
$pass = 'admin';
$dbname = 'simplebbs';

$char = 'utf8';

$lastq = false;

if($db=@mysql_connect($host, $user, $pass))
{
  if(@mysql_select_db($dbname,$db))
  {
  	//echo 'yes';
	mysql_query('SET NAMES '.$char.'');
	$res = array();
	if($lastq = mysql_query('select * from bbslistdata'))
	{
		echo 'yes';echo "\n<br>";
		echo @mysql_num_rows($lastq);
		while($row = @mysql_fetch_assoc($lastq))
		{
			$res[] = $row;
		}

	}
	print_r($res);

  }
  else
  {
    //$err.='';
  }
  mysql_close();
}

?>

Actually, it is just a short-hand method for puting data into an array through a loop. And after palying around with php.net array section, I found that it is a common manner in pratice.



 类似资料:

相关阅读

相关文章

相关问答