: This article mainly introduces the web interface of the SQL administrator. if you are interested in the PHP Tutorial, refer to it. /*************************************** **************************************** ******
* SQLAdmin v2.0-An SQL Administration User Interface for the Web *
* Copyright (C) 1997-98 Alessandro Vernet *
**************************************** **************************************** *****
* This library is free software; you can redistribute it and/or *
* Modify it under the terms of the GNU Library General Public *
* License as published by the Free Software Foundation; either *
* Version 2 of the License, or (at your option) any later version .*
**
* This library is distributed in the hope that it will be useful ,*
* But without any warranty; without even the implied warranty *
* MERCHANTABILITY or fitness for a special PURPOSE. See the GNU *
* Library General Public License for more details .*
**
* You shoshould have written ed a copy of the GNU Library General Public *
* License along with this library; if not, write to *
* Free Software Foundation, Inc., 59 Temple Place-Suite 330 ,*
* Boston, MA 02111-1307, USA .*
**************************************** **************************************** *****/
/* TODO:
*-Add sort order.
*-Add simple view.
*-Add some documentation.
*/
/* LIMITATIONS:
*-Works only with mSQL.
*/
/* HISTORY:
*-97-11-05 (avernet) Corrected a bug with quote.
*-98-01-01 (avernet) Added a sortColumn parameter
* AdministrationTable function.
*-98-03-14 (avernet) Added function addTable to enable users
* Add (but not modify) en entry to the database.
*-98-05-19 (avernet) Submitted to PX.
*-98-10-11 (avernet) Now SQLAdmin works with php3. The PHP2 version
* Will not be mainteained anymore.
*-98-10-11 (avernet) SQLAdmin is now distributed under the LGPL
* Instead of MPL.
*/
Function escapeforhtml ($ string)
{
$ Result = $ string;
// $ Result = ereg_replace ("\" "," ", $ result );
$ Result = ereg_replace ("<", "<", $ result );
$ Result = ereg_replace (">", ">", $ result );
Return $ result;
}
Function displayTuple ($ fieldsNumber, $ fieldNames,
$ FieldLengths, $ values, $ mode)
{
$ Result = "";
$ Result. = "";
Return $ result;
}
Function fieldFromType ($ text, $ type)
{
If ($ type = "int" | $ type = "uint" | $ type = "real ")
{$ Result = $ text ;}
Else
{$ Result = "'". AddSlashes ($ text )."'";}
Return $ result;
}
Function executeMsql ($ database, $ command)
{
/* Echo" ". $ Command ."";*/
Msql ($ database, $ command );
}
Function handleRemove ($ database, $ table, $ fieldsNumber,
$ FieldNames, $ fieldLengths, $ fieldTypes)
{
Global $ remove;
If ($ remove! = "")
{
$ Command = "delete from". $ 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 );
}
}
Function handleUpdate ($ 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 );
}
}
Function handleAdd ($ database, $ table, $ fieldsNumber,
$ FieldNames, $ fieldLengths, $ fieldTypes)
{
Global $ add;
If ($ add! = "")
{
$ Command = "insert into". $ 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 );
}
}
Function displayRemoveUpdate ($ database, $ table, $ sortColumn,
$ FieldsNumber, $ fieldNames, $ fieldLengths)
{
$ Result = "";
If ($ sortColumn! = "")
{$ SortColumn = "order by". $ 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;
}
Function displayAdd ($ fieldsNumber, $ fieldNames, $ fieldLengths)
{
$ Result = "";
$ FieldIndex = 0;
While ($ fieldIndex <$ fieldsNumber)
{
$ Values [$ fieldIndex] = "";
$ FieldIndex ++;
}
$ Result. = displayTuple ($ fieldsNumber, $ fieldNames,
$ FieldLengths, $ values, "add ");
Msql_close ();
Return $ result;
}
Function administrationTable ($ 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;
}
Function addTable ($ 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;
}
?>
The preceding section introduces the web interface of the SQL administrator, including related content. if you are interested in the PHP Tutorial.