欢迎来到福编程网,本站提供各种互联网专业知识!

一个SQL管理员的web接口

发布时间:2006-10-09 作者: 来源:转载

/*************************************************************************************

*SQLAdminv2.0-AnSQLAdministrationUserInterfacefortheWeb *

*Copyright(C)1997-98AlessandroVernet*

*************************************************************************************

*Thislibraryisfreesoftware;youcanredistributeitand/or*

*modifyitunderthetermsoftheGNULibraryGeneralPublic*

*LicenseaspublishedbytheFreeSoftwareFoundation;either*

*version2oftheLicense,or(atyouroption)anylaterversion.*

**

*Thislibraryisdistributedinthehopethatitwillbeuseful,*

*butWITHOUTANYWARRANTY;withouteventheimpliedwarrantyof*

*MERCHANTABILITYorFITNESSFORAPARTICULARPURPOSE.SeetheGNU*

*LibraryGeneralPublicLicenseformoredetails.*

**

*YoushouldhavereceivedacopyoftheGNULibraryGeneralPublic *

*Licensealongwiththislibrary;ifnot,writetothe*

*FreeSoftwareFoundation,Inc.,59TemplePlace-Suite330,*

*Boston,MA02111-1307,USA.*

*************************************************************************************/

/*TODO:

*-Addsortorder.

*-Addsimpleview.

*-Addsomedocumentation.

*/

/*LIMITATIONS:

*-WorksonlywithmSQL.

*/

/*HISTORY:

*-97-11-05(avernet)Correctedabugwithquote.

*-98-01-01(avernet)AddedasortColumnparameterto

*administrationTablefunction.

*-98-03-14(avernet)AddedfunctionaddTabletoenableusersto

*add(butnotmodify)enentrytothedatabase.

*-98-05-19(avernet)SubmittedtoPX.

*-98-10-11(avernet)NowSQLAdminworkswithPHP3.ThePHP2version

*willnotbemainteainedanymore.

*-98-10-11(avernet)SQLAdminisnowdistributedundertheLGPL

*insteadofMPL.

*/

functionescapeforhtml($string)

{

$result=$string;

//$result=ereg_replace(""",""",$result);

$result=ereg_replace("<","<",$result);

$result=ereg_replace(">",">",$result);

return$result;

}

functiondisplayTuple($fieldsNumber,$fieldNames,

$fieldLengths,$values,$mode)

{

$result="";

$result.="".

"";

$result.="";

$fieldIndex=0;

while($fieldIndex<$fieldsNumber)

{

$result.="".$fieldNames[$fieldIndex]."";

if($fieldLengths[$fieldIndex]<=128)

{

$result.="

$fieldNames[$fieldIndex].""VALUE="".

$values[$fieldIndex].""SIZE="64">";

}

else

{

$result.="

$fieldNames[$fieldIndex].""".

"COLS="64"ROWS="10"WRAP="virtual">".

escapeforhtml($values[$fieldIndex])."";

}

$result.="

$fieldNames[$fieldIndex].

""VALUE="".escapeforhtml($values[$fieldIndex])."">".

"";

$fieldIndex++;

}

$result.="";

if($mode=="modify")

{

$result.="";

$result.="";

}

else

{$result.="";}

$result.="";

return$result;

}

functionfieldFromType($text,$type)

{

if($type=="int"||$type=="uint"||$type== "real")

{$result=$text;}

else

{$result="'".AddSlashes($text)."'";}

return$result;

}

functionexecuteMsql($database,$command)

{

/*echo"".$command."


";*/

msql($database,$command);

}

functionhandleRemove($database,$table,$fieldsNumber,

$fieldNames,$fieldLengths,$fieldTypes)

{

global$remove;

if($remove!="")

{

$command="DELETEFROM".$table."WHERE";

$fieldIndex=0;

while($fieldIndex<$fieldsNumber)

{

$fieldName="old-".$fieldNames[$fieldIndex];

global$$fieldName;

$command.=$fieldNames[$fieldIndex]."=".

fieldFromType($$fieldName,$fieldTypes[$fieldIndex]);

if($fieldIndex!=$fieldsNumber-1)

{$command.="AND";}

$fieldIndex++;

}

executeMsql($database,$command);

}

}

functionhandleUpdate($database,$table,$fieldsNumber,

$fieldNames,$fieldLengths,$fieldTypes)

{

global$update;

if($update!="")

{

$command="UPDATE".$table."SET";

$fieldIndex=0;

while($fieldIndex<$fieldsNumber)

{

$fieldName=$fieldNames[$fieldIndex];

global$$fieldName;

$command.=$fieldName."=".

fieldFromType($$fieldName,$fieldTypes[$fieldIndex]);

if($fieldIndex!=$fieldsNumber-1)

{$command.=",";}

$fieldIndex++;

}

$command.="WHERE";

$fieldIndex=0;

while($fieldIndex<$fieldsNumber)

{

$fieldName="old-".$fieldNames[$fieldIndex];

global$$fieldName;

$command.=$fieldNames[$fieldIndex]."=".

fieldFromType($$fieldName,$fieldTypes[$fieldIndex]);

if($fieldIndex!=$fieldsNumber-1)

{$command.="AND";}

$fieldIndex++;

}

executeMsql($database,$command);

}

}

functionhandleAdd($database,$table,$fieldsNumber,

$fieldNames,$fieldLengths,$fieldTypes)

{

global$add;

if($add!="")

{

$command="INSERTINTO".$table."(";

$fieldIndex=0;

while($fieldIndex<$fieldsNumber)

{

$command.=$fieldNames[$fieldIndex];

if($fieldIndex!=$fieldsNumber-1)

{$command.=",";}

$fieldIndex++;

}

$command.=")VALUES(";

$fieldIndex=0;

while($fieldIndex<$fieldsNumber)

{

$fieldName=$fieldNames[$fieldIndex];

global$$fieldName;

$command.=fieldFromType($$fieldName,$fieldTypes[$fieldIndex]);

if($fieldIndex!=$fieldsNumber-1)

{$command.=",";}

$fieldIndex++;

}

$command.=")";

executeMsql($database,$command);

}

}

functiondisplayRemoveUpdate($database,$table,$sortColumn,

$fieldsNumber,$fieldNames,$fieldLengths)

{

$result="";

if($sortColumn!="")

{$sortColumn="ORDERBY".$sortColumn;}

$msqlresult=msql($database,"SELECT*FROM".$table.$sortColumn);

$tuplesNumber=msql_numrows($msqlresult);

$tupleIndex=0;

while($tupleIndex<$tuplesNumber)

{

$fieldIndex=0;

while($fieldIndex<$fieldsNumber)

{

$values[$fieldIndex]=msql_result($msqlresult,$tupleIndex,

$fieldNames[$fieldIndex]);

$fieldIndex++;

}

$result.=displayTuple($fieldsNumber,$fieldNames,

$fieldLengths,$values,"modify");

$tupleIndex++;

}

return$result;

}

functiondisplayAdd($fieldsNumber,$fieldNames,$fieldLengths)

{

$result="";

$fieldIndex=0;

while($fieldIndex<$fieldsNumber)

{

$values[$fieldIndex]="";

$fieldIndex++;

}

$result.=displayTuple($fieldsNumber,$fieldNames,

$fieldLengths,$values,"add");

msql_close();

return$result;

}

functionadministrationTable($database,$table,$sortColumn)

{

$result="";

msql_connect( "localhost");

$msqlresult=msql($database,"SELECT*FROM".$table);

$fieldsNumber=msql_numfields($msqlresult);

$msqlresult=msql_listfields($database,$table);

$fieldIndex=0;

while($fieldIndex<$fieldsNumber)

{

$fieldNames[$fieldIndex]=msql_fieldname($msqlresult,$fieldIndex);

$fieldLengths[$fieldIndex]=msql_fieldlen($msqlresult,$fieldIndex);

$fieldTypes[$fieldIndex]=msql_fieldtype($msqlresult,$fieldIndex);

$fieldIndex++;

}

handleRemove($database,$table,$fieldsNumber,$fieldNames,$fieldLengths,$fieldTypes);

handleUpdate($database,$table,$fieldsNumber,$fieldNames,$fieldLengths,$fieldTypes);

handleAdd($database,$table,$fieldsNumber,$fieldNames,$fieldLengths,$fieldTypes);

$result.=displayRemoveUpdate($database,$table,$sortColumn,$fieldsNumber,$fieldNames,

$fieldLengths);

$result.=displayAdd($fieldsNumber,$fieldNames,$fieldLengths);

return$result;

}

functionaddTable($database,$table)

{

$result="";

msql_connect( "localhost");

$msqlresult=msql($database,"SELECT*FROM".$table);

$fieldsNumber=msql_numfields($msqlresult);

$msqlresult=msql_listfields($database,$table);

$fieldIndex=0;

while($fieldIndex<$fieldsNumber)

{

$fieldNames[$fieldIndex]=msql_fieldname($msqlresult,$fieldIndex);

$fieldLengths[$fieldIndex]=msql_fieldlen($msqlresult,$fieldIndex);

$fieldTypes[$fieldIndex]=msql_fieldtype($msqlresult,$fieldIndex);

$fieldIndex++;

}

handleAdd($database,$table,$fieldsNumber,$fieldNames,$fieldLengths,$fieldTypes);

$result.=displayAdd($fieldsNumber,$fieldNames,$fieldLengths);

return$result;

}

?>

相关推荐