";
}
function DB()
// makes database connection once and returns that connection for use
{global $CONFIG;
static $ret;
if ($ret===NULL) $ret=mysqli_connect($CONFIG['ServerName'], $CONFIG['UserName'], $CONFIG['Password'], $CONFIG['Database']);
return $ret;
}
function html_QueryStatus($in_Error)
// shows status of query that was run from the page
{$ret="\n\n\t\t
Query Successful
\n\n\t\t
";
$ret.="The submitted query was run without issue and its results can be viewed below.";
}
else
{$ret.="FF0000\">Query Error\n\n\t\t
";
$ret.="This tool will only run SELECT queries and the query submitted ";
if ($in_Error=="SELECTFROM")
// No select or from clause
{$ret.="did not contain a 'SELECT' or 'FROM' keyword so it wasn't ";
$ret.="sent to the server.";
}
elseif (substr($in_Error, 0, 7)=="Invalid")
// invalid character
{$ret.="failed a test that flags possible SQL injections. Queries run with this ";
$ret.="tool cannot contain the string '".substr($in_Error, 8)."'.";
}
else
// error from server will display it
{$ret.="was returned in error by the server. Below is the specific error ";
$ret.="message:
".$in_Error."";
}
}
return $ret;
}
function html_ConnectionStatus($in_ConnectionError, $in_QueryError)
// shows connection status in upper left area
{global $CONFIG;
$bool_Valid=True;
if (strlen($in_ConnectionError)>1) $bool_Valid=False;
// determines if error present or not
$ret="\n\n\t\t
Connection Successful";
else $ret.="FF0000\">Connection Error";
$ret.="
\n\n\t\t
";
// displays color coded alert about connection
if ($bool_Valid)
// connection valid, will display that message
{$ret.="The user name, password and database ('".$CONFIG['Database']."') information submitted was ";
$ret.="accepted by the MySQL server ('".$CONFIG['ServerName']."') and a connection was made. ";
$ret.="The Query Tool section to the right can now be used to run queries against the database.";
}
else
// issue with connection will explain
{$ret.="There are 3 connection steps to this tool--contacting the server, accepting the credentials ";
$ret.="and accessing the specific database. This connection ";
if (strpos($in_ConnectionError, "getaddrinfo failed")!==false)
// failed at server info
{$ret.="failed immediately because it was not able to even find '";
$ret.=$CONFIG['ServerName']."'. Make sure 'ServerName' is typed correctly in the Configuration ";
$ret.="Area of this file.";
}
else if (strpos($in_ConnectionError, "using password:")!==false)
// failed at credentials
{$ret.="passed the first step and connected to the server ('".$CONFIG['ServerName'];
$ret.="'), but the server did not accept the credentials. Make sure 'UserName' and 'Password' are ";
$ret.="typed correctly in the Configuration Area of this file. Also, make sure those credentials ";
$ret.="are set up and have the proper authorization on the server itself.";
}
else if (strpos($in_ConnectionError, "to database")!==false)
// failed at database
{$ret.="passed the first step and connected to the server ('".$CONFIG['ServerName'];
$ret.="', passed the second step and the credentials were accepted but failed to connect to ";
$ret.="the '".$CONFIG['Database']."' database. Make sure 'Database' is typed correctly ";
$ret.="in the Configuration Area of this file. Also, make sure those credentials ";
$ret.="are set up and have the proper authorization to that specific database itself.";
}
else
{$ret.="failed at an unknown step. Below is the raw error message:";
$ret.="\n\t\t\t
".$in_ConnectionError."";
}
}
$ret.="
View All Connection Data";
if (($bool_Valid) && ($in_QueryError!="NoQuery")) $ret.=html_QueryStatus($in_QueryError);
// if connection valid will show query status area
return $ret;
}
// ++++++++++++++++++ Above: Data functions
// ++++++++++++++++++ Below: Variables
$SQL['SQL']=""; // sql statement to be executed
$SQL['Query']=array(); // result back from mysql server of query
$SQL['Result']=array(); // array of results
$SQL['MaxResults']=50; // maximum number of results query will show, will add LIMIT to any SQL
$SQL['Rows']=-1; // number of rows returned from query
$SQL['Tables']=array(); // holds all tables in database
$SQL['Error_Connect']=""; // error message about connection to database
$SQL['Error_Query']="NoQuery"; // error message about specific query user submitted
$SQL['Status_Connect']=1; // status of connection, 1=valid, 0=invalid
$SQL['Status_Query']=0; // status of query, 0=no query, 1=valid, 2=invalid, 3=show conn data
$SQL['Status']=1; // status of page
//0=invalid connection; 1=valid connection no query; 2=valid connection & query; 3=valid connection & invalid query; 4=show all conn data
$counter_i=0; // counters to loop through data
$str_Invalid=""; // holds which invalid word SQL contains and caused it to fail
$arr_Invalids=array("DELETE", "INSERT", "ALTER", "TRUNCATE", "DROP", "UPDATE", ";");
// invalid input terms for user submitted query, page only runs SELECT queries
// ++++++++++++++++++ Above: Variables
// ++++++++++++++++++ Below: Data processing
DB();
$SQL['Error_Connect']=mysqli_connect_error();
if (strlen($SQL['Error_Connect'])>1) $SQL['Status_Connect']=0;
// initializes database connection, gets any errors and updates status
if ($SQL['Status_Connect']==1)
// successful connection will pull all tables from database for use in dropdown
{$tmp_query=mysqli_query(DB(), "SHOW TABLES FROM ".$CONFIG['Database'].";");
while ($r = mysqli_fetch_array($tmp_query)) $SQL['Tables'][]=$r[0];
}
if (isset($_GET['in_Connection']))
// user wants all connection data
{$SQL['Status_Query']=3;
$SQL['Result']=array(array("Function", "Value"), array("get_host_info", mysqli_get_host_info(DB())),
array("get_server_version", mysqli_get_server_version(DB())), array("get_server_info", mysqli_get_server_info(DB())),
array("connect_errno", mysqli_connect_errno()), array("connect_error", mysqli_connect_error()),
array("get_client_version", mysqli_get_client_version()), array("get_client_info", mysqli_get_client_info(DB())),
array("get_proto_info", mysqli_get_proto_info(DB())), array("sqlstate", mysqli_sqlstate(DB())),
array("stat", mysqli_stat(DB())));
$counter_maxi=count($SQL['Result']);
for ($counter_i=1; $counter_i<$counter_maxi; ++$counter_i)
// will change first element of each function into link to w3school for it
{$tmp_Function="mysqli_".$SQL['Result'][$counter_i][0];
$SQL['Result'][$counter_i][0]="".$tmp_Function."()";
}
}
if ($SQL['Status_Connect']==1)
// successful connection will run queries on it
{if (isset($_GET['in_Table']))
// user submitted table to get structure of
{$SQL['Status_Query']=1;
$SQL['SQL']="SHOW FULL COLUMNS FROM ".mysqli_real_escape_string(DB(), $_GET['in_Table'])." FROM ".$CONFIG['Database'];
}
if (isset($_POST['in_SQL']))
// user submitted SQL
{$SQL['Status_Query']=1;
$SQL['SQL']=trim($_POST['in_SQL']);
if (substr($SQL['SQL'], -1)==";") $SQL['SQL']=substr($SQL['SQL'], 0, -1);
// if last character a semicolon, removes it
if ((strtoupper(substr($SQL['SQL'], 0, 6)) !== "SELECT") || (strpos(strtoupper($SQL['SQL']), 'FROM') === false))
// makes sure submitted query starts with SELECT and contains FROM
{$SQL['Error_Query']="SELECTFROM";
$SQL['Status_Query']=2;
}
if ($SQL['Status_Query']==1)
// valid SQL so far, will make sure its limited to $SQL['MaxResults'] results at most
{$tmp_int=strpos($SQL['SQL'], " LIMIT ");
if ($tmp_int)
// SQL already contains LIMIT keyword, will make it less than $SQL['MaxResults'] if over $SQL['MaxResults']
{$str_tmp=substr($SQL['SQL'], $tmp_int + 7);
$tmp_int=strpos($str_tmp, " ");
if ($tmp_int) $str_tmp=substr($str_tmp, 0, $tmp_int);
// extracts numerical value of LIMIT
if ($str_tmp>$SQL['MaxResults']) $SQL['SQL']=str_replace(" LIMIT ".$str_tmp, " LIMIT ".$SQL['MaxResults'], $SQL['SQL']);
// if submitted limit is over $SQL['MaxResults'] replaces it with $SQL['MaxResults']
}
else $SQL['SQL'].=" LIMIT ".$SQL['MaxResults'];
}
}
if ($SQL['Status_Query']==1)
// user requested SQL be run, will validate it
{foreach ($arr_Invalids as $tmp_str) if (strpos(strtoupper($SQL['SQL']), $tmp_str)>0) $str_Invalid=$tmp_str;
if (strlen($str_Invalid)>0)
{$SQL['Status_Query']=2;
$SQL['Error_Query']="Invalid-".$str_Invalid;
}
}
if ($SQL['Status_Query']==1)
// query passes pre-checks will execute
{$SQL['Error_Query']="Valid";
$SQL['Query']=mysqli_query(DB(), $SQL['SQL']);
$str_tmp=mysqli_error(DB());
if (strlen($str_tmp)>1)
{$SQL['Status_Query']=2;
$SQL['Error_Query']=$str_tmp;
}
}
}
if ($SQL['Status_Query']==1)
// query successfully run will load results into $SQL['Result']
{$SQL['Rows']=mysqli_num_rows($SQL['Query']);
if ($SQL['Rows']>0) while ($r = mysqli_fetch_assoc($SQL['Query']))
{if (count($SQL['Result'])==0) $SQL['Result'][]= array_keys($r);
$SQL['Result'][]=$r;
}
}
// ++++++++++++++++++ DATA PROCESSING ABOVE ++++++++++++++++++
// ++++++++++++++++++ PAGE OUTPUT BELOW ++++++++++++++++++
?>
Simple MySQL Query Tool
Database Status
This tool uses MySQLi
(https://www.php.net/manual/en/book.mysqli.php)
to connnect and interact with a MySQL database. The below area details the specifics of this particular
connection and any query that was run.
|
Query Tool
Use the below area to execute a SELECT query against the database.
Or select a table of the database to view its structure.
|
Query Results
Below is the SQL submitted to the database and then the results of that query.
This tool automatically limits any query to just results which is why a LIMIT appears in the SQL
even if one was not submitted. That can be modified by changing the $SQL['MaxResults'] value in the Variables area of this script.
".html_Results($SQL['Result']); ?>
Below is all the connection meta-data returned by various MySQLi diagnostic functions. Each function below
also serves as a link to a help page for that function.