php+mssql 已经写好的万能函数


<?php
/**************************************************************************************************
| 类名:databaseClass
| 功能描述:操作数据库。内有函数link_array(),equation_array(),insert(),update(),delete(),query()
| 初始化数据: $select(要操作的数据库名),$link(已经打开的一个数据库链接)
| 建立日期:2006-7-21
| 完成日期:
| 修改日期:
| 其他说明:
|
|**************************************************************************************************/

class databaseCortrol{

public $dataKeyList;
public $show;
public $result;
public $num_rows;

function __construct($select,$link){
mssql_select_db($select);
$this->link=$link;
}
function __destruct(){
}
/**************************************************************************************************
| 函数名:link_array()
| 功能描述:将数组用特定的形式连接成字符串
| 输入参数: $arrayList(数组或单个变量),$partition(连接符),$headAdd,$trailAdd--各连接数头、尾添加符
| 返回值:return $string(返回修改后的字符串)
| 建立日期:2006-7-21
| 完成日期:
| 修改日期:
| 程序员:浪迹天涯/v
| 其他说明:
|
|**************************************************************************************************/
function link_array($arrayList,$partition,$headAdd="",$trailAdd=""){
if(is_array($arrayList)){
$num=count($arrayList);
for($i=0;$i<$num-1;$i++){
$string.=$headAdd.$arrayList[$i].$trailAdd.$partition;
}
$string.=$headAdd.$arrayList[$num-1].$trailAdd;
return $string;
}else{
$string=$headAdd.$arrayList.$trailAdd;
return $string;
}
}
/**************************************************************************************************
| 函数名:equation_array()
| 功能描述:将两个数组或两个数按索引相等,并用特定的形式连接成字符串
| 输入参数: $arrayKey(数组或单个变量),$arrayValue(数组或单个变量),$partition(连接符),$headAdd,$trailAdd--各连接数头、尾添加符
| $equation,$valueHead,$valueTrial--作为update()函数调用时可省去后5位参数,使用默认值
| 返回值:return $string(返回修改后的字符串)
| 建立日期:2006-7-21
| 完成日期:
| 修改日期:
| 程序员:浪迹天涯/v
| 其他说明arrayKey数组与$arrayValue数组的个数不相等时,可能数据不完整
|
|**************************************************************************************************/
function equation_array($arrayKey,$arrayValue,$partition,$headAdd="",$trailAdd="",$equation="=",$valueHead="‘",$valueTrail="‘"){
$keyNum=count($arrayKey);
$valueNum=count($arrayValue);
$num=(($keyNum-$valueNum)>0)?($valueNum):$keyNum;
if($num>1){
for($i=0;$i<$num-1;$i++){
$string.=$headAdd.$arrayKey[$i].$equation.$valueHead.$arrayValue[$i].$valueTrail.$trailAdd.$partition;
}
$string.=$headAdd.$arrayKey[$num-1].$equation.$valueHead.$arrayValue[$num-1].$valueTrail.$trailAdd;
return $string;
}else if($num==1){
$string=$headAdd.$arrayKey.$equation.$valueHead.$arrayValue.$valueTrail.$trailAdd;
return $string;
}else{
return false;
}
}
/**************************************************************************************************
| 函数名:array_insert()
| 功能描述:可将变量插入到数组的中指定位置(功能与系统函数array_splice()差不多.)如$array1=array_insert($array,2,"kk")
| 同于array_splice($array2,2,0,"kk"),两函数运行后$array1=$array2
| 输入参数:
| 返回值:return $string(返回修改后的数组)
| 建立日期:2006-8-9
| 完成日期:
| 修改日期:
| 程序员:浪迹天涯/v
| 其他说明:未完成2006-08-09
|
|**************************************************************************************************/
function array_insert($array,$serial,$instead){
if(is_array($serial)){
}else{
for($i=0;$i<count($array);$i++){
if($i==$serial){
$tmp[$i]=$array[$i];
$array[$i]=$instead;
}else if($i>$serial){
$tmp[$i]=$array[$i];
$array[$i]=$tmp[$i-1];
}
}
}
return $array;
}
/**************************************************************************************************
| 函数名:array_trim()
| 功能描述:将数组中各个单元分别去两头空格
| 输入参数: $array数组(支持多维数组及单个字符串);
| 返回值:return $array_trim(返回修改后的数组)
| 建立日期:2006-08-17
| 完成日期: 2006-08-17
| 修改日期: 2006-08-24(改进原来去多维数组空格时出错)
| 程序员:浪迹天涯/v
| 其他说明:
|
|**************************************************************************************************/
function array_trim($array){
if(is_array($array)){
foreach ($array as $key=>$value) {
if(is_array($value)){
$trim_key=trim($key);
$trim=$this->array_trim($value);
$array_trim[$trim_key]=$trim;
}else{
$trim_key=trim($key);
$trim=trim($value);
$array_trim[$trim_key]=$trim;
}
}
}else{
$array_trim=trim($array);
}
return $array_trim;
}

/**************************************************************************************************
| 函数名:insert()
| 功能描述:对数据库数据表插入数据
| 输入参数data_table(数插入数据的表名)。
$data_file(数据插入的字段名。当$data_file="",时$data_value必须是带字段索引的字符串,即按字段索引来给对应的字段付值)。
$data_value(数据插入的值)。
$flag(是否引许$data_file与$data_value个数不等当$flag="1"时可以不等,但$data_value不能大于$data_file,若$data_value为带字段索引,字段索引须包含在$data_file中,若此时$data_file为空,则应包含在插入的表的字段中。当$flag="2"时,$data_value必须是带字段索引数组,$data_value可以大于$data_file,当有$data_value的索引不是有效字段时,忽略该数据.)。
$flag2(默为0自动去字段及键值两头空格,当$flag2=1时不去空格)。
| 返回值:返回插入数据影响记录的条数,若插入失败返回0
| 建立日期:2006-7-21
| 完成日期:
| 修改日期:2006-08-10
| 程序员:浪迹天涯/v
| 其他说明:当$data_file与$data_value个数不等时函数将
|
|**************************************************************************************************/
function insert($data_table,$data_file="",$data_value,$flag="0",$flag2="0"){
if($data_file==""){
//$qu_query="select Top 1 * from $data_table";//查询$data_table表第一行
//@$qu_result=mssql_query($qu_query) or die("插入数据时查寻列名时出错了!!!");
//$num_fields=mssql_num_fields($qu_result);//返回字段个数
//for($i=0;$i<$num_fields;$i++){
//$data_file[$i]=mssql_field_name($qu_result,$i);//取得表中的字段名,0表示取第一个字段名,1取第二个,以此类推
//echo mssql_field_type($qu_result,$i);
//}
$qu_query="select a.name from syscolumns a,systypes b where a.xtype=b.xtype and a.id=object_id(‘".$data_table."‘)";//查询表中的字段
$qu_query2="select a.name from syscolumns a,systypes b where a.xtype=b.xtype and a.id=object_id(‘".$data_table."‘) and COLUMNPROPERTY( a.id,a.name,‘IsIdentity‘)=1";//查询表中的自增量字段(由于不等于1时求出非自增量字段时索引乱了,只好用总字段-自增字段
@$qu_result=mssql_query($qu_query) or die("插入数据时查寻列名出错,请检查数据库类,或者尝试指定插入的字段名!!");
@$qu_result2=mssql_query($qu_query2) or die("插入数据时查寻列名出错,请检查数据库类,或者尝试指定插入的字段名!!");
$i=0;
while($row=mssql_fetch_array($qu_result,MSSQL_ASSOC)){//用 MYSQL_ASSOC 只得到关联索引,如同 mysql_fetch_assoc()。
$tmpFile[$i]=$row[name];
$i++;
}
//print_r($tmpFile);
$j=0;
while($row=mssql_fetch_array($qu_result2,MSSQL_ASSOC)){//用 MYSQL_ASSOC 只得到关联索引,如同 mysql_fetch_assoc()。
$tmpFile2[$j]=$row[name];
$j++;
}
$data_file=array_values(array_diff($tmpFile,$tmpFile2));
/**if($flag=="1"){
if(array_intersect($data_file,array_values($data_value))=array_values($data_value)){
}else{

}
} **/
}
$num_file=count($data_file);
$num_value=count($data_value);
if($flag=="1" && $num_file!=$num_value){
if($flag="2"){
}else{
if($num_file>$num_value){
$data_value=array_pad($data_value,$num_file,"");//在数组后面追加空值,使$data_file与$data_value相等
}else{
echo "出错了,你输入的值大于字段个数!!";
return false;
}
}
}else{
if($num_file!=$num_value){
echo "出错啦!!!insert对象第二个($data_file)及第三个参数($data_value)个数不相等!";
return false;
}
}
if($flag2!="1"){
$data_file=$this->array_trim($data_file);
$data_value=$this->array_trim($data_value);
}
$file=$this->link_array($data_file,",");
$value=$this->link_array($data_value,",","‘","‘");
$query="insert into $data_table ($file)values($value);";
//echo $query;
@$result=mssql_query($query) or die ("保存数据时出错了!!!");
$num_chang= mssql_rows_affected($this->link);
@mssql_free_result($result);
return $num_chang;
}
/**************************************************************************************************
| 函数名:update()
| 功能描述:对数据库数据表更新数据
| 输入参数data_table(更新数据的表名),$data_file(数据更新的字段名),$data_value(数据更新的值),$data_check(数据更新条件),$flag2默为0自动去字段及键值两头空格,当$flag2=1时不去空格
| 返回值:返回更新数据影响记录的条数,若更新失败返回0
| 建立日期:2006-7-21
| 完成日期:
| 修改日期:
| 程序员:浪迹天涯/v
| 其他说明:
|
|**************************************************************************************************/
function update($data_table,$data_file,$data_value,$data_check="",$flag="0",$flag2="0"){
if($data_check!="" || $flag=="1"){
if(count($data_file)==count($data_value)){
if($flag2!="1"){
$data_file=$this->array_trim($data_file);
$data_value=$this->array_trim($data_value);
}
$data_file_value=$this->equation_array($data_file,$data_value,",");
$queryUpate="UPDATE $data_table SET $data_file_value $data_check";
//echo $queryUpate;
@$resultUpate=mssql_query($queryUpate) or die("数据更新时出错了");
$num_chang= mssql_rows_affected($this->link);
return $num_chang;
}else{
echo "出错啦!!!update对象第二个($data_file)及第三个参数($data_value)个数不相等!";
return false;
}
}else{
echo "出错啦!!!当$data_check参数为空时,应当将$flag改为1才可以将所有数据进行修改";
}
}
/**************************************************************************************************
| 函数名:delete()
| 功能描述:对数据库数据表进行删除操作
| 输入参数:$data_table(删除数据的表名),$data_check(数据删除条件)
| 返回值:返回数据删除的条数,若删除失败返回0
| 建立日期:2006-7-21
| 完成日期:
| 修改日期:
| 程序员:浪迹天涯/v
| 其他说明:
|
|**************************************************************************************************/
function delete($data_table,$data_check="",$flag="0"){
if($data_check!="" || $flag=="1"){
$queryDelete="DELETE from $data_table $data_check";
//echo $queryDelete;
@$result=mssql_query($queryDelete) or die("删除数据时出错了!!!");
$num_chang= mssql_rows_affected($this->link);
return $num_chang;
}else{
echo "出错啦!!!delete()函数$data_check为空时$flag应为1才可以删除所有数据";
}
}
/**************************************************************************************************
| 函数名:query()
| 功能描述:对数据库数据表进行查寻操作
| 输入参数:$data_table(查寻数据的表名),$data_file(查寻数据的字段名),$data_check(数据查寻条件),$dataKeyList(数据查寻后返回的数组索引规则,默认则等于$data_file,当$dataKeyList="autocoding"时,返回的字段键值为顺序序号,$flag2默为0自动去字段及键值两头空格,当$flag2=1时不去空格
| 返回值:返回数据查寻到的条数,但注意查寻文件不合条件或查寻失败都返回0
| 建立日期:2006-7-21
| 完成日期:
| 修改日期:
| 程序员:浪迹天涯/v
| 其他说明:
|
|**************************************************************************************************/
function query($data_table,$data_file="",$data_check="",$dataKeyList="",$flag2="0"){
//global $select_count;
//global $show;
if($flag2!="1"){
$data_file=$this->array_trim($data_file);
$dataKeyList=$this->array_trim($dataKeyList);
}
if($data_file=="")$data_file="*";
$file=$this->link_array($data_file,",");
//print_r($data_table);
$query="select $file from $data_table $data_check";
echo "sql 语句: ".$query."<br>";
@$result= mssql_query($query) or die("数据查寻时出错了!!!");
$this->result=@$result;
// @$res=mssql_fetch_array($result);
// $rownum = mysql_num_rows($result);
$num_rows = mssql_num_rows($result);//返回数据集行数
$num_fields=mssql_num_fields($result);//返回字段个数
if($num_rows>0){
if($dataKeyList=="" && $data_file=="*"){
for($i=0;$i<$num_fields;$i++){
$dataKeyList[$i]=mssql_field_name($result,$i);//取得表中的字段名,0表示取第一个字段名,1取第二个,以此类推
}
$data_file=$dataKeyList;
//$yy=mssql_fetch_field($result);
//print_r($yy->max_length);
//print_r($dataKeyList);
//echo "<br>";
}elseif($dataKeyList==""){
$dataKeyList=$data_file;
}elseif($dataKeyList=="autocoding"){
$dataKeyList=range(0, conunt($data_file));
}
$file_count=count($data_file);
$file_count2=count($dataKeyList);
if($file_count<$file_count2 && $data_file!="*")$dataKeyList=$data_file;
$select_count=0;
while($row=mssql_fetch_array($result,MSSQL_ASSOC)){//加MSSQL_ASSOC后不返回$row[0]等数字索引只得关联索引
for($i=0;$i<$file_count2;$i++){

// echo "strncasecmp=".strncasecmp($dataKeyList[$i],"count(*)",8)."=<br>" ;
// if($dataKeyList[$i]=="count(*)" || strncasecmp($dataKeyList[$i],"count(*)",8)==0)
if(strlen($dataKeyList[$i])>8&&(strncasecmp($dataKeyList[$i],"count(*)",8)==0||strncasecmp($dataKeyList[$i],"count(1)",8)==0))
{
$arrkey=explode(" ",$dataKeyList[$i]);
$show[$select_count][$dataKeyList[$i]]=$row[$arrkey[2]];
}
elseif($dataKeyList[$i]=="count(*)"||$dataKeyList[$i]=="count(1)")
{
$show[$select_count][$dataKeyList[$i]]=$row["computed"];
}
else
{
$show[$select_count][$dataKeyList[$i]]=$row[$data_file[$i]];
}
}
if($file_count>$file_count2){
for($j=$i;$j<count($data_file);$j++){
$show[$select_count][$dataKeyList[$j]]=$row[$data_file[$j]];
}
}
$select_count++;
}/**
if(is_array($dataKeyList)){
}else{
$file_count=count($dataKeyList);
$select_count=0;
while($row=mssql_fetch_array($result,MSSQL_ASSOC)){
for($i=0;$i<$file_count;$i++){
$show[$select_count][$dataKeyList]=$row[$data_file[$i]];
}
$select_count++;
}
}**/
}
mssql_free_result($result);
if($flag2!=1 && $show!=""){
$show=$this->array_trim($show);
}
$this->dataKeyList=$dataKeyList;
$this->show=$show;
$this->select_count=$select_count;
$this->num_rows=$num_rows;
return $num_rows;
}

function displayquery()
{

$dataKeyList=$this->dataKeyList; //fields name
$show=$this->show; //data value
$select_count=$this->select_count; //

if ($show)
{

$output= "<p class=‘four‘><table ><tr>";
foreach($dataKeyList as $field)
$output=$output."<p class=‘one‘><td>".$field."</td></p>";
$output=$output."</tr>";

foreach($show as $k=>$val)
{//意思是for $book each $value( as )
$output=$output."<tr>";
if( is_array($val) )
foreach( $val as $value)
$output=$output. "<p class=‘four‘><td> ".$value."</td></p>";
else
$output=$output. "<p class=‘one‘><td>".$val."</td></p>";
$output=$output. "</tr>";
}
$output=$output. "</table></p>";

}else
{

$output="no result";
}

echo $output;

}
}

?>
<style type="text/css">
p.one
{
border-style: solid;
border-color: #0000ff
}
p.two
{
border-style: solid;
border-color: #ff0000 #0000ff
}
p.three
{
border-style: solid;
border-color: #ff0000 #00ff00 #0000ff
}
p.four
{
border-style: solid;
border-color: #ff0000 #00ff00 #0000ff rgb(250,0,255)
}
</style>

php+mssql 已经写好的万能函数,布布扣,bubuko.com

时间: 2024-08-24 19:14:59

php+mssql 已经写好的万能函数的相关文章

[C++] 用Xcode来写C++程序[4] 函数

用Xcode来写C++程序[4] 函数 此节包括引用函数,内联函数,防止修改函数入参,函数自身带有默认值. 引用函数:防止复制对象,减少系统开销 内联函数:编译的时候根据具体情形将代码嵌入进去,成不成功编译器说了算,减少系统开销提升性能 引用函数(防止篡改初始值的入参声明方式):防止修改数据源 函数参数带有默认值:函数的某个参数可以给定默认值,精简函数的使用 最简单的函数 #include <iostream> using namespace std; int addition (int a,

[C++] 用Xcode来写C++程序[5] 函数的重载与模板

用Xcode来写C++程序[5] 函数的重载与模板 此节包括函数重载,隐式函数重载,函数模板,带参数函数模板 函数的重载 #include <iostream> using namespace std; int operate (int a, int b) { return (a * b); } double operate (double a, double b) { return (a / b); } int main () { int x = 5; int y = 2; double n

写好Python之函数

写好Python之函数 避免使用可变对象作为参数德默认值 当Python解释器对函数定义时,通过默认参数表达式来判断他们的值.该表达式仅发生一次.调用该函数不会触发其他的参数表达式值.由于计算的值用于所有函数调用序列,使用可变对象作为默认值会发生一些未期待的结果. 可变对象意味值可以直接进行修改.list, dict, set或大部分类实例.我们可以使用调用append来追加list元素. 不可变对象,如其名,意为创建后不可修改,string, int, 和tuple对象都是不可变对象.我们不能

数据结构与算法-字符串写出一个strlen函数

写出一个strlen函数 int strlen( const char *str ) //输入参数const { assert( str != NULL ); //断言字符串地址非0 int len=0; while( (*str++) != '' ) { len++; } return len; }

跟我一起写操作系统(三)——打印函数和qemu虚拟机

转载注明出处:http://www.cnblogs.com/lucasysfeng/p/4847662.html 上一讲地址:http://www.cnblogs.com/lucasysfeng/p/4847662.html 项目地址:https://github.com/lucasysfeng/lucasOS 本讲主要涉及以下三个方面:1. 打印函数封装:2. qemu的使用:3. 项目目录结构调整. 理由如下:1. 后续开发无疑会遇到打印变量的操作,因此有必要封装打印函数(内核开发阶段,无法

一步一步写算法(之函数堆栈显示)

原文:一步一步写算法(之函数堆栈显示) [ 声明:版权所有,欢迎转载,请勿用于商业用途.  联系信箱:feixiaoxing @163.com ] 在继续图的讨论之前,我们今天开个小差,讨论一下函数堆栈的基本原理.有过编程经验的朋友都知道,堆栈调试是我们在程序开发中经常应用的一个功能.那么大家有没有想过,函数堆栈是怎么开始的啊?其实我们可以自己写一个函数堆栈输出函数分析一下. 因为一般来说,函数的压栈过程是这样的: |    参数三  | |    参数二  | |    参数一  | |  

写的一个split函数

vector<string> strsplit(const string& str) { vector<string> vec; string sstr1=str, sstr2=""; size_t np=0; while (sstr1!="") { size_t dt = sstr1.find(','); if (dt != string::npos) { sstr1 = str.substr(np, dt); sstr2 = st

写一个求和的函数sum

写一个求和的函数sum,达到下面的效果 // Should equal 15 sum(1, 2, 3, 4, 5); //Should equal 0 sum(5, 'abc', -5); //Should equal 4 sum(1, true, 'a', 'D', 1, 'F', 1, 'w'): 下面附上多种方法: function sum(){ var add = 0; for(var i = 0; i < arguments.length; i++){ if(!isNaN(Number

代写存储过程、SQL函数语句

代写存储过程.SQL函数语句Each team is most likely to have the following tables in the existing model. Use the Excel data provided with this phase link: PATIENT (some teams called it EMPLOYEE or CUSTOMER) - If needed, alter your PATIENT table to include the fiel