php学习之mysqli的面向对象

// mySqlTool.php  封装好的工具类

<?php
class SqlTool{
	private $conn;
	private $host="localhost";
	private $user="root";
	private $password="Aa1234";
	private $db="cw_system";
	
	 function SqlTool(){
		$this->conn=mysqli_connect($this->host,$this->user,$this->password);
		if(!$this->conn)
		{
			die("连接数据库失败".mysqli_error());
			
		}
	mysqli_select_db($this->conn,$this->db);
	mysqli_query($this->conn,"set names utf8");
		
	}
	//方法
	//完成Select sql
	public function  execute_dql($sql) {
		
		$res=mysqli_query($this->conn,$sql) or die(mysqli_error());
		return $res;
	}
	
	//完成Update ,delete,insert,dml
	public function execute_dml($sql)
	{
		$b=mysqli_query($this->conn,$sql ) or die(mysqli_error());
		if(!$b)
		{
			
		return 0; //失败	
		}
		else 
		{
			if(mysqli_affected_rows($this->conn)>0)
			{
				
			return  1; //表示成功
			}
			else {
			return 2;//表示没有行数影响	
				
			}
			
			
		}
				
		
	}
	
	
	
}
?>

  

//addApplayInfo.php

<?php
require_once dirname(__FILE__)."/../model/mySqlTool.php";
header("Content-type: text/html;charset=utf-8");	
$sql="select * from kq_apply";

$sqlTool=new SqlTool();
$res=$sqlTool->execute_dql($sql);
while($row=$res->fetch_row())
{
	foreach ($row as $key=>$val)
	{
		
	echo"--$val--";	
	}
	echo "<br/>";
}
$res->free();  
// get account num
// $num = count($_POST['applydateTime']);
// var_dump($_POST['applydateTime']);
// for($i=0;$i<$num;$i++){
// 	$No='K1082';
// 	$deptNo=$_POST['applydateTime'][$i];
// 	$applyTypeId=$_POST['applykbName'][$i];
// 	$startTime=$_POST['startTime'][$i];
// 	$endTime=$_POST['endTime'][$i];
// 	$extraTime=$_POST['reason'][$i];

// 	$idresult=mysqli_query($con,$idSearchSql);
// 	$idArr=mysqli_fetch_array($idresult);
// 	var_dump($idArr);
// 	//释放内存
// 	mysqli_free_result($idresult);
// }


?>

  

posted @ 2017-03-17 09:09  公众号python学习开发  阅读(170)  评论(0编辑  收藏  举报