<?php
/**
 * Database Constants - these constants are required
 * in order for there to be a successful connection
 * to the MySQL database. Make sure the information is
 * correct.
 */
define("DB_SERVER", "myServer eg:localhost");
define("DB_USER", "myUserName");
define("DB_PASS", "myPassword");
define("DB_NAME", "myDatabaseName");
### program start #############################################################
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta http-equiv="keywords" content="database,fields,display,records">
<title>Database Editor</title>
</head>
<body>
<a  href="http://snippets.bluejon.co.uk/index.php"/>MENU</a> <br />
<br /><br />
<div style= "width:430px;">
<?php
### connection to mysql #######################################################
if($connect= @mysql_connect(DB_SERVER,DB_USER,DB_PASS)){
  $connStatus = "Connected to server";
}else{
    $connErr= true;
    $connStatus ="Unable to connect to sever";
}
### check for database ########################################################
if($db= @mysql_select_db(DB_NAME,$connect)){
  $dbStatus= "Connected to database";
}else{
  $dbErr= true;
    $dbStatus= mysql_error();
}
?>

<form name= 'dbEditorForm' action= "<?php print $PHP_SELF?>" method= 'post'>
<table  border='1' cellpadding='2' cellspacing='0' width= '430'>
<tr><td colspan= '3'><center>DATABASE &nbsp; INFORMATIOM</center> </td></tr>

<?php
if($connErr== true or $dbErr== true){
    print"<tr><td>Server connection status</td>";  
  if($connErr== true){
        print"<td style='color:red'>$connStatus";  
    }else{
      print"<td>$connStatus";
    }
    print"</td></tr>";
    print"<tr><td>Database status</td>";  
  if($dbErr== true){
        print"<td style='color:red'>$dbStatus";  
    }else{
      print"<td>$dbStatus";
    }
    print"</td></tr>";
     print"<tr><td colspan= '3'>";
    print"What to do!.<br />";
    print"To display database information.<br />";
  print"Copy the source code into a text editor.<br />";
  print"Change the top of the file to get the connection to the server.<br />";
    print"Save the file with a php extension.<br />";
  print"Put the file in root of your website<br />";
  print"With a browser goto your site.<br />";
  print"Type the new file name at the end of your url<br /> ";
  print"Do another search";
    print"</td></tr>";
    print"</table>";
}else{
    ###### db table list #####################################################
    $dbTables = mysql_query("SHOW TABLES FROM ".DB_NAME);
    print"<tr> <td rowspan= 6> 
  <select name='dbEditorTableSel' size='8'>";
     while ($dbTable = mysql_fetch_row($dbTables)){
       foreach($dbTable as $dbTableName){
           print "<option value= '$dbTableName'>$dbTableName</option>";
             }
     }

    print"</select>";
    print"</td>"; 
    ###### db info process #####################################################
  if(isset($_POST['dbEditorTableSel'])){
        $dbEditorTableSel= $_POST['dbEditorTableSel']; 
        $query= "SELECT * FROM ".$dbEditorTableSel;
      $dbTableData= mysql_query($query)or die("invalid  query:" .mysql_error());
      $dbServerVer= mysql_get_server_info();
        $dbFieldCount= mysql_num_fields($dbTableData);
        $dbRecordCount= mysql_num_rows($dbTableData);
      ###### db info stats #####################################################
        print"<td>Server version</td>";
      print"<td>$dbServerVer</td>";
         print"</tr><tr>";
        print"<td>Table name</td>";
      print"<td>$dbEditorTableSel</td>";
      print"</tr><tr>";
        print"<td>Field count</td>";
      print"<td>$dbFieldCount</td>";
      print"</tr><tr>";
      print"<td>Record count</td>";
      print"<td>$dbRecordCount</td>";
    }else{
        print"<td height= '100'>&nbsp;</td>";  
        print"<td>&nbsp;</td>";
    }
    print"</tr><tr>";
    print"<td valign= 'bottom' style='width:100%;' colspan= '2'><center><input type= 'submit' name= 'dbEditorTable'value= 'Select' ></center></td>"; 
  print"</tr>";
    print"</table>";
    print"</form>";
    ### dispay database information ########################################
    if(isset($_POST['dbEditorTableSel'])){
        print"<table  border='1' cellpadding='2' cellspacing='0'>";
        print"<tr>";
        for($i=0; $i<$dbFieldCount; $i++){/* field names */  
            print"<td>". mysql_field_name($dbTableData, $i). "</td>";      
        }
        print"</tr>";
      print"<tr>";
        for($i=0; $i<$dbFieldCount; $i++){/* field type */    
            print"<td>". mysql_field_type($dbTableData, $i). "</td>";      
        }
        print"</tr>";
      print"<tr>";
        for($i=0; $i<$dbFieldCount; $i++){/* field length */    
            print"<td>". mysql_field_len($dbTableData, $i). "</td>";      
        }
        print"</tr>";
        ### dispay table information ###########################################
        while ($dbFields = mysql_fetch_row($dbTableData)){
        print"<tr>";
          foreach($dbFields as $dbField){
          if($dbField){
                  print "<td>$dbField</td>"; 
                }else{
                     print "<td>&nbsp;</td>"; 
                }    
            }
         print"</tr>";
         }
       print"</table>";
    }
}

?>
</div>
<br /><br />
<a  href="http://snippets.bluejon.co.uk/db-editor/db-viewer-v1-phpcode.php">Display php source code </a> 
</body>
</html>