面向对象来实现数据库操作

/*一个文件MySQLDB.class.php中包含了一个类来连接数据库、
执行语句的方法、
将查询到的结果集通过数组返回的方法。

*/
host = isset($config['host']) ? $config['host'] : 'localhost';
		$this->port = isset($config['port']) ? $config['port'] : '3306';
		$this->username = isset($config['username']) ? $config['username'] : 'root';
		$this->password = isset($config['password']) ? $config['password'] : 'forlove123';
		$this->charset = isset($config['charset']) ? $config['charset'] : 'utf8';
		$this->dbname = isset($config['dbname']) ? $config['dbname'] : 'newphp';
		
		//连接数据库的函数调用,没件事独立使用
		$this->connect();
		//设定连接编码
		$this->setCharset($this->charset);
		//选定数据库
		$this->selectDb($this->dbname);
	}
	public static function getInstance($config){
		if(!isset(self::$link)){
			self::$link = new self($config);		//参数问题
		}
		return self::$link;
	} 
	//禁止克隆
	private function __clone(){}
	public function connect(){
		$this->resource = mysql_connect("$this->host:$this->port", "$this->username", "$this->password") or die("连接数据库失败");
	}
	public function setCharset($charset){
		//mysql_set_charset($charset, self::$link);
		mysql_query("set names $charset");	//设置编码失败易于显示错误
	}
	public function selectDb($dbname){
		//mysql_select_db($dbname, self::$link);
		mysql_query("use $dbname");		//选择数据库失败易于显示错误
	}
	/*
	功能执行sql语句
	返回,失败直接结束,成功则返回执行结果
	*/
	public function query($sql){
		if(!$result = mysql_query($sql, $this->resource)){
			echo	"
执行语句失败"; echo "
执行失败语句为:".$sql; echo "
出错信息为:".mysql_error(); echo "
错误代号为:".mysql_errno(); die(); } return $result; } /* 执行select语句返回二维数组 参数:$sql 字符串类型 select语句 */ public function getAll($sql){ $result = $this->query($sql); $arr = array(); while( $rec = mysql_fetch_assoc( $result )){ $arr[] = $rec; } return $arr; } public function getRow($sql){ $result = $this->query($sql); while( $rec = mysql_fetch_assoc( $result )){ //返回下标为字段名的下标 //如果取得了一行数据 return $rec; } return false; } public function getOne($sql){ $result = $this->query($sql); //结果集为0行也不会判断为false, 它是资源 $rec = mysql_fetch_row($result); //返回下标为数字的数组,且下标一定为0,1,2,3 if($rec == false){ return false; } return $rec[0]; } } /* 测试文件:MysqlDb_use.php */ 'localhost', 'port' => '3306', 'username' => 'root', 'password' => 'forlove123', 'charset' => 'utf8', 'dbname' => 'newphp' ); //设置连接数据库信息 $link = MySQLDB::getInstance( $config ); //单例话一个对象那个 //var_dump($link); //任务1:插入数据: $sql = "insert into money(zhanghu, cunkuan) values ('test3', 10000)"; if( $link ->query($sql) ) echo "执行成功"; //任务2获取账户所有信息 $sql = "select * from money"; $result = $link->getAll($sql); echo "
";
//print_r($result);
echo	"";
	foreach ($result as $rows){
		echo	"";
			foreach ($rows as $key => $value){
				echo	"";
			}
		echo	"";
	}
echo	"
$value
"; echo "
"; //任务3:获取某个账户信息(一行数据): $sql = "select * from money where id = 1"; $result = $link->getRow($sql); //得到一维数组 if($result){ echo "
账户ID:".$result['id']; echo "
账户名:".$result['zhanghu']; echo "
存款为:".$result['cunkuan']; } else echo "
账户不存在!!!"; //获取银行的存款总和 $sql = "select sum(cunkuan) as s from money "; $result = $link->getOne($sql); if($result) echo "
总存款为:".$result; else echo ""; ?>

测试结果为:

面向对象来实现数据库操作_第1张图片

你可能感兴趣的:(学web)