Thinkai's Blog

Autohoutkey|Python|php|aardio|VOIP|IT 爱好者

正在浏览分类 pdo

总共找到 1 篇

<?php
//连接
$c = new PDO( "sqlsrv:server=(local); Database = ", "sa", "123456", array(PDO::SQLSRV_ATTR_DIRECT_QUERY => true)); 


//返回不含字段名称的查询
function query($query){
	global $c;
	//定义全局变量
	$stmt = $c->prepare( $query, array(PDO::ATTR_CURSOR => PDO::CURSOR_SCROLL));
	$stmt->execute();
	global $rowcount;
	$rowcount=NULL;
	//判断sql类型
	 $rowcount = $stmt->rowCount();
	if(stripos($query, 'update') !== false){
	      return "UP".$rowcount; 
	}elseif(stripos($query, 'delete') !== false){
	      return "DE".$rowcount; 
	}elseif(stripos($query, 'insert') !== false){
	      return "IN".$rowcount; 
	}else{
	//初始化变量
	$content=NULL;
	if( $stmt->rowCount() == 0){
	      return "";
	}else{
	   while ( $row = $stmt->fetch( PDO::FETCH_NUM ) ){
		for($i = 0; $i < $stmt->columnCount(); $i++)
	     {
	          $type=gettype($row[$i]);
	          //判断是否object类型
	          if ($type=="object"){
	          	 $var=$row[$i];
	          	 //格式化时间
	          	 $time=str_replace("T"," ",$var->format(DateTime::ISO8601));
	          	 $time=substr($time,0,19);
	          	 $time=$time.".000"; 
	         	 $content=$content.$time.",";
	          }else{
	          	  $ctmp=str_replace(",",",",$row[$i]);
	          	  $content=$content.$ctmp.",";
	          	  //追加
	          }
	     }
	     $content=$content."\n";
	}
	$content=str_replace(",\n","\n",$content);
	return $content;
	}
	}
}


//返回含字段名称的查询
function qt($query){
	global $c;
	$stmt = $c->prepare( $query, array(PDO::ATTR_CURSOR => PDO::CURSOR_SCROLL));
	$stmt->execute();
	global $rowcount;
	$rowcount=NULL;
	 $rowcount = $stmt->rowCount();
	if(stripos($query, 'update') !== false){
	      return "UP".$rowcount; 
	}elseif(stripos($query, 'delete') !== false){
	      return "DE".$rowcount; 
	}elseif(stripos($query, 'insert') !== false){
	      return "IN".$rowcount; 
	}else{
	$content=NULL;
	if( $stmt->rowCount() == 0){
	      return "NO";
	}else{
	 $a = $stmt->fetch( PDO::FETCH_ASSOC);
	 $Fields = array_keys($a); 
	 global $titles;
	 $titles=NULL;
	 //循环获取字段名称
	 for($i = 0; $i < $stmt->columnCount(); $i++)
		{
		$name=iconv('GB2312','UTF-8',$Fields[$i]);
		$titles=$titles."$name,";
		} 
		$titles=$titles."\r\n";
		$titles=str_replace(",\r\n","",$titles);
		$content = $titles."\r\n";
	   $stmt->execute();
	   while ( $row = $stmt->fetch( PDO::FETCH_NUM ) ){
		for($i = 0; $i < $stmt->columnCount(); $i++)
	     {
	          $type=gettype($row[$i]);
	          if ($type=="object"){
	          	 $var=$row[$i];
	          	 $time=str_replace("T"," ",$var->format(DateTime::ISO8601));
	          	 $time=substr($time,0,19);
	          	 $time=$time.".000"; 
	         	 $content=$content.$time.",";
	          }else{
	          	  $ctmp=str_replace(",",",",$row[$i]);
	          	  $content=$content.$ctmp.",";
	          }
	     }
	     $content=$content."\r\n";
	}
	$content=str_replace(",\r\n","\r\n",$content);
	return $content;
	}
	}
}


//仅执行
function get($sql){
	global $c;
	global $result;
	$result = $c->prepare( $sql, array(PDO::ATTR_CURSOR => PDO::CURSOR_SCROLL));
	$result->execute();
	global $rowcount;
	$rowcount=NULL;
	 $rowcount = $result->rowCount();
}
?>



友情链接:Autohotkey中文论坛Autohotkey中文帮助Autohotkey官网我的B站直播间如若生涯一场梦博客联系作者免GooglePlay APK下载

 主题设计 • skyfrit.com  Thinkai's Blog | 保留所有权利

49 queries in 0.577 seconds |