@ 2014-05-08T18:12:34Z <?php
// Author: Carl Turechek
define( 'DBTYPE', 'mysql');
define( 'DBHOST', 'localhost');
define( 'DBNAME', 'database');
define( 'DBUSER', 'root');
define( 'DBPASS', '');
define( 'DBSCHEMA','');
function get_dbconn($dbname = DBNAME) {
$dbDat = new stdClass;
$dbDat->name = $dbname;
try {
$dbh = new PDO( sprintf("%s:host=%s;",DBTYPE,DBHOST),DBUSER,DBPASS);
$dbs = $dbh->query("SHOW DATABASES LIKE '$dbname'");
if( ( $db = $dbs->fetchColumn( 0 ) ) !== false )
{
//echo $db.'<br>';
if(!$dbh2 = new PDO( sprintf("%s:host=%s;dbname=%s",DBTYPE,DBHOST,$dbname),DBUSER,DBPASS)){
echo "PDO Error 1.2:\n";print_r($pdo->errorInfo());
}else{
$dbDat->pdo = $dbh2;
return $dbDat;//return pdo object if table name is found
}
}
} catch (PDOExeption $e){
echo $e->getmessage();
}
}
class dbCommands{
/**** connect and create $dbname if no $pdo object present****/
function createDB($pdo,$dbname){
if (!$pdo) {//if no db then create, previously checked
try {
$dbh = new PDO( sprintf("%s:host=%s;",DBTYPE,DBHOST),DBUSER,DBPASS);
$dbh->exec("CREATE DATABASE $dbname;")
/* CREATE USER '$user'@'localhost' IDENTIFIED BY '$pass';
GRANT ALL ON `$db`.* TO '$user'@'localhost';
FLUSH PRIVILEGES;*/
or die(print_r($dbh->errorInfo(), true));
} catch (PDOException $e) {
die("DB ERROR: ". $e->getMessage());
}echo "Database $dbname created successfully\n";
echo"<form action='' method='get'>";
echo'<input type="hidden" name="db" value="',$dbname,'">
<input type="submit">
</form>';
}else{
echo'Connected to:',$dbname;echo'<br>';
}
}
/**** delete databases ****/
function deleteDBs($pdo){
if($pdo and @$_GET['removeDatabase']){
try {
if($pdo->exec( "DROP DATABASE IF EXISTS {$_GET['removeDatabase']}" )){
}
echo'<div style="float:left;border: solid #000 1px;display: inline-block;"><h2>dropping ',$dbname,'</h2>';
}catch (PDOException $e) {
echo $e->getMessage();
}
}
}
/**** show helper for db and table links ****/
function showHelper($id,$type){
$link='';
if($type=='db'){$link.=$type.'='.$id;
return'<a href="?'.$link.'">'.$id.'</a><br>';
}
if($type=='table'){$link.='db='.$_GET['db'].'&'.$type.'='.$id;}
return'<a href="?'.$link.'">'.$id.'</a><a style="float:right;color:#f00;" href="?db='.$_GET['db'].'&removeTableName='.$id.'">X</a><br>';
}
/**** show all databases ****/
function showDBs($pdo){
$dbLinks='';
if($pdo){
$type='database';
echo'<div style="float:left;border: solid #000 1px;display: inline-block;"><h2>All Databases</h2>';
$this->displayAddRemove($type);
$dbs = $pdo->query( 'SHOW DATABASES' );
while( ( $db = $dbs->fetchColumn( 0 ) ) !== false )
{
$dbLinks.=$this->showHelper($db,'db');
}
echo $dbLinks,'</div>';
}
}
/*** Add / Remove helper for dbs / tables */
function displayAddRemove($type){
if($type=='table'){
echo'<div style="float:left;border: solid #000 1px;display: inline-block;"><h2>All tables in '.$_GET['db'].'</h2>';
echo"<a href='?db={$_GET['db']}&addTable=true'>add</a> - <a href='?db={$_GET['db']}&removeTable=true'>remove</a><hr>";
/* show remove table form*/
if(@$_GET['addTable']==='true'){
echo"<form action='' method='get'>";
echo'Name: <input type="text" name="addTableName"><br>
<input type="hidden" name="db" value="',$_GET["db"],'">
<input type="submit">
</form>';
}
/* show remove table form*/
if(@$_GET['removeTable']==='true'){
echo"<form action='' method='get'>";
echo'Name: <input type="text" name="removeTableName"><br>
<input type="hidden" name="db" value="',$_GET["db"],'">
<input type="submit">
</form>';
}
}
if($type=='database'){
echo"<a href='?addDB=true'>add</a> - <a href='?removeDB=true'>remove</a><hr>";
/* show remove table form*/
if(@$_GET['addDB']==='true'){
echo"<form action='' method='get'>";
echo'Name: <input type="text" name="addDatabase"><br>
<input type="submit">
</form>';
}
/* show remove table form*/
if(@$_GET['removeDB']==='true'){
echo"<form action='' method='get'>";
echo'Name: <input type="text" name="removeDatabase"><br>
<input type="submit">
</form>';
}
}
}
/**** show all tables in $dbname w/ add remove forms****/
function showTables($pdo){
$tblLinks='';
if($pdo and isset($_GET['db'])){
$type='table';
$this->displayAddRemove($type);
if(!$tbls = $pdo->query( 'SHOW TABLES' )){
echo "PDO Error 1.2:\n";print_r($pdo->errorInfo());
}else{
while( ( $tbl = $tbls->fetchColumn( 0 ) ) !== false )
{
$tblLinks.=$this->showHelper($tbl,'table');
}
echo $tblLinks;
}
echo'</div>';
}
}
/**** add tables in $dbname ****/
function addTable($pdo){
$tblLinks='';
if($pdo and (@$_GET['addTableName'])){
echo'<div style="border: solid #000 1px;"><h2>Creating table ',$_GET['addTableName'],'</h2>';
if($pdo->exec( "CREATE TABLE {$_GET['addTableName']}(id INT(10) NOT NULL AUTO_INCREMENT PRIMARY KEY)ENGINE=MyISAM AUTO_INCREMENT=0 DEFAULT CHARSET=utf8;" )){
echo "PDO Error 1.2:\n";print_r($pdo->errorInfo());
}else{
echo'added!';
}
echo'</div><br>';
}
}
/**** drop tables in $dbname ****/
function removeTable($pdo){
$tblLinks='';
if($pdo and (@$_GET['removeTableName'])){
echo'<div style="border: solid #000 1px;"><h2>Dropping table ',$_GET['removeTableName'],'</h2>';
if(false===$pdo->exec( "DROP TABLE {$_GET['removeTableName']}" )){
echo "PDO Error 1.2:\n";print_r($pdo->errorInfo());
}else{
echo'dropped!';
}
echo'</div><br>';
}
}
/***** Display Table Content*****/
function showTableRows($pdo){
function getColumns($pdo, $reqType){
if(!$rows = $pdo->query( "SHOW COLUMNS FROM {$_GET['table']}")) {
echo "PDO Error 1.2:\n";print_r($pdo->errorInfo());
}else{
$all = $rows->fetchAll();
//print_r($all);
foreach($all as $key => $value){
if($test=explode('(',$value['Type'])){}else{$test=$value['Type'];}
$allTypes[$key]['type']=$test[0];
$size=explode(')',@$test[1]);
$allTypes[$key]['size']=$size[0];
$allTypes[$key]['name']=$value['Field'];//build array for later use
//output fields to be added
if($reqType !=='getAllArray'){
if(($test[0]==='int' or $test[0]==='bigint') and ($value['Field'] !=='id')){
echo '<div style="display:inline-block">',$test[0],'-',$value['Field'],'-',$size[0],': <br><input type="text" name="',$value['Field'],'"></div>';
}
if($test[0]==='varchar' or $test[0]==='text' or $test[0]==='tinytext' or $test[0]==='mediumtext' or $test[0]==='longtext'){
echo '<div style="display:inline-block">',$test[0],'-',$value['Field'],'-',$size[0],': <br><textarea type="text" name="',$value['Field'],'"></textarea></div>';
}
}
}
if($reqType =='getAllArray'){ return $allTypes;
}
}
}
if($pdo and (@$_GET['table'])){
echo'<div style="float:left;border: solid #000 1px;display: inline-block;"><h2>All rows in ',$_GET['table'],'</h2>';
echo"<a href='?db={$_GET['db']}&table={$_GET['table']}&addRow=true'>add</a> Rows<hr>";//rows
echo"<a href='?db={$_GET['db']}&table={$_GET['table']}&addCol=true'>add</a> - <a href='?db={$_GET['db']}&table={$_GET['table']}&removeCol=true'>remove</a> Columns<hr>";//columns
/* show add row form*/
if(@$_GET['addRow']==='true'){
echo"<form action='' method='post'>";
getColumns($pdo, @$reqType);
echo'<input type="hidden" name="db" value="',$_GET["db"],'"><input type="hidden" name="table" value="',$_GET["table"],'"><input type="hidden" name="addingRow" value="true">
<input type="submit">
</form>';
}
/* show remove column form*/
if(@$_GET['addCol']==='true'){
echo"<form action='' method='get'>";
echo'Name: <input type="text" name="addColName"><br>
After: <input type="text" name="afterColName"><br>
Type: <input list="addColType" name="addColType">
<datalist id="addColType">
<option value="int">int</option>
<option value="bigint">bigint</option>
<option value="text">text</option>
<option value="char">char</option>
<option value="varchar">varchar</option>
<option value="timestamp">timestamp</option>
</datalist><br>
Size: <input type="text" name="addColSize"><br>
<input type="hidden" name="db" value="',$_GET["db"],'"><input type="hidden" name="table" value="',$_GET["table"],'">
<input type="submit">
</form>';
}
/* show remove row form*/
if(@$_GET['removeCol']==='true'){
echo"<form action='' method='get'>";
echo'Name: <input type="text" name="removeColName"><br>
<input type="hidden" name="db" value="',$_GET["db"],'"><input type="hidden" name="table" value="',$_GET["table"],'">
<input type="submit">
</form>';
}
$reqType='getAllArray';
$allTypes=getColumns($pdo, $reqType);
echo'<h3>Showing Table: ',$_GET['table'],'</h3>
<form action="" method="post" ><table cellpadding="2" cellspacing="2" border="1" class="display" id="example" style="text-align:left;">
<thead>
<tr>';
echo'<th>delete</th>';
foreach($allTypes as $key => $value){
echo"<th><input type='text' name='column-head-{$value['name']}[]' value='{$value['name']}'>";
echo"<input type='text' style='width:30px;' name='column-size-[]' value='{$value['size']}'>
<input list='column-type-' name='column-type-[]' value='{$value['type']}'>
<datalist id='column-type-'>
<option value='int'>int</option>
<option value='bigint'>bigint</option>
<option value='text'>text</option>
<option value='char'>char</option>
<option value='varchar'>varchar</option>
<option value='timestamp'>timestamp</option>
</datalist><br>
</th>";
}
echo'</tr>
</thead>
<tbody>';
if(!$rows = $pdo->query( "SELECT * FROM {$_GET['table']}" )){
echo "PDO Error 1.2:\n";print_r($pdo->errorInfo());
}else{ $rows->setFetchMode(PDO::FETCH_ASSOC);
while(( $row = $rows->fetch( )) !== false ){
echo'<tr>';
foreach($row as $key => $value){
if($key=='id'){echo'<input type="hidden" name="pageIds[]" value="',$value,'">';
echo '<td align="center"><input type="checkbox" name="delete[]" value="',$value,'"></td>';
echo '<td>',$value,'</td>';
}else{
echo '<td><pre><textarea type="text" name="',$key,'[]" >';
$value = str_replace("&", "&" ,$value);
echo $value = str_replace("<", "<",$value);
echo '</textarea></pre></td>';
}
}
echo'</tr>';
}
echo'
</tbody>
</table><p><input type="hidden" name="db" value="',$_GET["db"],'"><input type="hidden" name="table" value="',$_GET["table"],'"><input type="submit" name="update" value="Make Changes"></p>
</form>';
}
echo'</div>';
}
}
/**** add row into table ****/
function addRow($pdo){
if($pdo and (@$_GET['table']) and (@$_POST['addingRow'])){
echo'<div style="border: solid #000 1px;"><h2>adding row </h2>';
foreach($_POST as $key => $value){
if($key !== 'table' and $key !== 'db' and $key !== 'addingRow'){
$qk[]=$key;
$qv[]=$pdo->quote($value);
}
}
$q="INSERT INTO {$_POST['table']} (";
$q.=implode(',',$qk);
$q.=") VALUES (";
$q.=implode(',',$qv);
$q.=")";
if($pdo->exec( "$q" )){
}else{
echo "PDO Error 1.2:\n";print_r($pdo->errorInfo());
echo'added!';
}
echo'</div><br>';
}
}
/**** add column to table ****/
function addCol($pdo, $after_column = 'id'){
if($pdo and (@$_GET['addColName'])){
if(@$_GET['afterColName']!==''){$after_column = @$_GET['afterColName'];}
echo'<div style="border: solid #000 1px;"><h2>Adding Column ',$_GET['addColName'],'</h2>';
$typeSize =$_GET['addColType'];
if(@$_GET['addColSize'] !=''){
$typeSize.='('.$_GET['addColSize'].')';
}
if($pdo->exec( "ALTER TABLE {$_GET['table']} ADD {$_GET['addColName']} $typeSize AFTER $after_column" )){//maybe add type
echo "PDO Error 1.2:\n";print_r($pdo->errorInfo());
}else{
echo'Column added!';
}
echo'</div><br>';
}
}
/**** remove column from table ****/
function removeCol($pdo){
if($pdo and (@$_GET['removeColName']) and $_GET['removeColName'] !== 'id'){
echo'<div style="border: solid #000 1px;"><h2>removing Column '.$_GET['removeColName'].'</h2>';
if($pdo->exec( "ALTER TABLE {$_GET['table']} DROP {$_GET['removeColName']}" )){//add type
echo "PDO Error 1.2:\n";print_r($pdo->errorInfo());
}else{
echo'Column: ',$_GET['removeColName'],' removed!';
}
echo'</div><br>';
}
}
/**** update columns ****/
function updateColumns($pdo){
if($pdo and (@$_POST['update'])){
echo'<div style="border: solid #000 1px;"><h2>updating columns</h2>';
foreach($_POST as $key => $value){
if('column-head-' == substr($key, 0, 12)){
$newname[] = $value[0];$oldname[] =substr($key, 12);
}
}
$i=0;
foreach($_POST['column-type-'] as $key => $value){
if($i!==0){
$typesize =$value;
if(@$_POST['column-size-'][$i]){
$typesize.='('.$_POST['column-size-'][$i].')';
}
if($pdo->exec( "ALTER TABLE {$_GET['table']} CHANGE {$oldname[$i]} {$newname[$i]} $typesize" )){
// echo "PDO Error 1.2:\n";print_r($pdo->errorInfo());
}
}
$i++;
}
echo'</div><br>';
}
}
/**** update table ****/
function update($pdo){
if($pdo and (@$_POST['table']) and (@$_POST['update'])){
echo'<div style="border: solid #000 1px;"><h2>updating </h2>';
$i=0;
foreach($_POST as $key => $value){ $i=0;
if($key !== 'table' and $key !== 'db' and $key !== 'update' and $key !== 'id' and $key !== 'pageIds' and $key !== 'delete' and 'column-head-' !== substr($key, 0, 12) and 'column-size-' !== substr($key, 0, 12) and 'column-type-' !== substr($key, 0, 12)){
foreach($value as $key1 => $value1){
$toUpdate[$_POST['pageIds'][$i++]][$key]=$value1;
}
}
}
unset($value); unset($value1);
foreach($toUpdate as $key => $value){
$i=0;
$query="UPDATE {$_GET['table']} SET ";
foreach($value as $column => $value1){
$ColandVal[]="$column=".$pdo->quote($value1);
}
$query.=implode(',',$ColandVal);
$query.=" WHERE id='$key';";echo'<br>';
if($pdo->exec( "$query" )){
echo'added!';//echo "PDO Error 1.2:\n";print_r($pdo->errorInfo());
}else{
}
}
echo'</div><br>';
}
}
//****---delete-row
function delRows($pdo){
if(@$_POST['delete'])
foreach(@$_POST['delete'] as $key => $value){
if($pdo->exec( "DELETE FROM {$_GET['table']} WHERE id = $value" )){
echo'deleted!';// echo "PDO Error 1.2:\n";print_r($pdo->errorInfo());
}
}
}
}
/*-*-* end classes--
start program *-*-*/
$dbCommands = new dbCommands();
if(isset($_GET['addDatabase']) and $_GET['addDatabase']!==''){$dbname=@$_GET['addDatabase'];}else{$dbname=@$_GET['db'];}
$dbDat=get_dbconn($dbname); if($dbDat !== null){$pdo=$dbDat->pdo;$dbname=$dbDat->name;}
$dbCommands->createDB($pdo,$dbname);// create db $dbname if no connection to could be made.
$dbCommands->deleteDBs($pdo);
$dbCommands->addTable($pdo);
$dbCommands->removeTable($pdo);
$dbCommands->addRow($pdo);
$dbCommands->addCol($pdo);
$dbCommands->update($pdo);
$dbCommands->updateColumns($pdo);
$dbCommands->removeCol($pdo);
$dbCommands->delRows($pdo);
$dbCommands->showDBs($pdo);
if($pdo and $dbname){
$dbCommands->showTables($pdo);
$dbCommands->showTableRows($pdo);
}
?>
Enable javascript to submit You have javascript disabled. You will not be able to edit any code.
Output for git.master_jit , git.master , rfc.property-hooks Fatal error: Uncaught PDOException: could not find driver in /in/KFrKV:16
Stack trace:
#0 /in/KFrKV(16): PDO->__construct('mysql:host=loca...', 'root', Object(SensitiveParameterValue))
#1 /in/KFrKV(503): get_dbconn(NULL)
#2 {main}
thrown in /in/KFrKV on line 16
Process exited with code 255 . This tab shows result from various feature-branches currently under review by the php developers. Contact me to have additional branches featured.
Active branches Archived branches Once feature-branches are merged or declined, they are no longer available. Their functionality (when merged) can be viewed from the main output page
preferences:dark mode live preview
84.07 ms | 401 KiB | 8 Q