• 欢迎访问开心洋葱网站,在线教程,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站,欢迎加入开心洋葱 QQ群
  • 为方便开心洋葱网用户,开心洋葱官网已经开启复制功能!
  • 欢迎访问开心洋葱网站,手机也能访问哦~欢迎加入开心洋葱多维思维学习平台 QQ群
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏开心洋葱吧~~~~~~~~~~~~~!
  • 由于近期流量激增,小站的ECS没能经的起亲们的访问,本站依然没有盈利,如果各位看如果觉着文字不错,还请看官给小站打个赏~~~~~~~~~~~~~!

一个php mysql数据库操作类

PHP 水墨上仙 2917次浏览

一个php mysql数据库操作类,功能不是很完善,但非常有用

<?php
class Database{
	private $host;
	private $user;
	private $pwd;
	private $rows;
	private $error;
	private $result;	
	private $dbName;
	private $connection;	
	private $isReady;
 
	public function __construct(){ 		
		$this->result = null;
		$this->isReady = false; 
		$this->error = array();
		}
	public function __destruct(){ @mysql_close($this->connection); }
 
	/* setters */
	public function setHost($host){ $this->host = $host; }
	public function setUser($user){ $this->user = $user; }
	public function setPassword($pwd){ $this->pwd = $pwd; }
	public function setDbName($dbName){ $this->dbName = $dbName; }
 
	/* other interfaces */
	public function init($host=null,$user=null,$pwd=null,$dbName=null){
		if(!isset($host,$user,$pwd,$dbName)) 
			die("Please provide require settings.");
		$this->setHost($host);
		$this->setUser($user);
		$this->setPassword($pwd);
		$this->setDbName($dbName);
		$this->isReady = true;
		}
 
	public function select($dbName){
		$this->setDbName($dbName);
		mysql_select_db($this->dbName,$this->connection) or die("The said database does not exist.");
		}
 
	public function query($sql){
		$this->result = mysql_query($sql,$this->connection) or die("Invalid query string!");
		}	
 
	public function connect(){
		if(!$this->isReady) die("not ready to connect");
		$this->connection = mysql_connect($this->host,$this->user,$this->pwd) or die("Could not connect to database. please check your credentials.");
		$this->select($this->dbName);
		$this->query("SET NAMES 'utf8'",$this->connection); //persian support
		}	
 
	public function isConnected(){
		if($this->connection) 
			return true;
		return false;
		}
 
	public function disconnect(){
		mysql_close($this->connection); 
		$this->connection = null;
		}
 
	public function countRows($selectMode = true){
		if($selectMode)
			return mysql_num_rows($this->result);
		return mysql_affected_rows($this->connection);
		}
 
	public function loadRows(){
		if(!$this->result) die("Nothing found!");
		$this->rows = array();
		while($r = mysql_fetch_array($this->result,MYSQL_BOTH))
			$this->rows[] = $r;
		mysql_free_result($this->result);
		return $this->rows;
		}
 
	public function siftDown($dataStack){
		if(!is_array($dataStack)){
			$dataStack = ereg_replace("[\'\")(;|`,<>]","",$dataStack);
			$dataStack = mysql_real_escape_string(trim($dataStack),$this->connection);
			$dataStack = stripslashes($dataStack);
			return $dataStack;
			}
		$safeData = array();
		foreach($dataStack as $p=>$data){
					$data = ereg_replace("[\'\")(;|`,<>]","",$data);
					$data = mysql_real_escape_string(trim($data),$this->connection);
					$data = stripslashes($data);
					$safeData[$p] = $data;
					}
		return $safeData;
		}
 
	public function secure($data){
		return sha1(md5(sha1(md5(sha1($data)))));
	}
}//Database class
?>
 
<?php //usage
	require_once 'path/to/Database.class.php';
	$db = new Database(); //Creating new object
	$db->init("localhost","test_root","test_pwd!","test_db"); //initializing by credentials.
	$db->connect(); //unicode support
	$test_value = $db->siftDown($test_value); //preventing harmful inputs
	$something_testy_else = $db->siftDown($something_testy_else);
	$db->query("SELECT * FROM test_table WHERE test_field = '$test_value' AND second_test_field = '$something_testy_else' LIMIT 1");
	if($db->countRows()==1)
  		$dbdata = $db->loadRows(); //returns a numeric/associative array as the result (MYSQL_BOTH)
	//TODO: To Process $dbdata
	$db->disconnect();
?>


开心洋葱 , 版权所有丨如未注明 , 均为原创丨未经授权请勿修改 , 转载请注明一个php mysql数据库操作类
喜欢 (0)
加载中……