Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

mySQL Explorer version 1 1

Status
Not open for further replies.

KarveR

MIS
Dec 14, 1999
2,065
GB
Hi all,

I have a real old mysql database, I've been throwing stuff at it for so long I couldn't remember what I had in it.

Using the command line to go through it all was taking forever and not really showing me the info that was there.

I made the following page to get all the names of the databases, and tables within them into select boxes and then display the data held in each when selected.

Now with a couple of clicks I can explore my mySQL databases easily.

Just thought others here may find it useful.

Feel free to post any bugs or suggestions and I'll chang it if it seems like a good idea.

Currently developed on PHP version 4.0.6

----------------mysqlexplore.php----------------------------
<!DOCTYPE HTML PUBLIC &quot;-//W3C//DTD HTML 4.0 Transitional//EN&quot;>
<html>
<head>
<title>mySQL Explorer - Version: 1.0</title>
</head>
<body topmargin=&quot;0&quot; leftmargin=&quot;0&quot; marginheight=&quot;0&quot; marginwidth=&quot;0&quot;
bgcolor=&quot;#ffffff&quot; text=&quot;#000000&quot; link=&quot;#000099&quot; alink=&quot;#0000ff&quot;
vlink=&quot;#000099&quot;>

<?php
//////////////////////////////////////////////////////////////////////////////////
// //
// mySQL Explorer version 1 Copyright Karv@virtualkev.com //
// //
// Written : 16th June 2002 //
// Last modified: never //
// Bugs : none it seems (yet) //
// //
//////////////////////////////////////////////////////////////////////////////////


// Database connection details - CHANGE THESE AS NECESSARY //

$host=&quot;localhost&quot;;
$user=&quot;root&quot;;
$password=&quot;&quot;;

//////////////////////////////////////////////////////////////////////////////////
// You should not need to edit anything below here unless you use PHP Version > 4.0.6
// and are experiencing problems. If so please email me details or problems to fix.
//////////////////////////////////////////////////////////////////////////////////

// connect

$connection=@mysql_connect($host,$user,$password) ;

$err_no=mysql_errno();

if ($err_no > 0 ){

echo &quot;<h2> Error:</h1> &quot; . mysql_errno() . &quot;: &quot; . mysql_error() . &quot;<br>&quot;;

exit;

}

// start the page

echo &quot;<table border=0 width=100% cellspacing=1 cellpadding=0>\n&quot;;

echo &quot;<tr bgcolor=#0099cc>\n<td wdith=34%>\n&quot;;


// find databases;

echo &quot;<form name=select_db action=$PHP_SELF>\n&quot;;

$db_list = mysql_list_dbs($connection);

echo &quot; <select name=db>\n&quot;;

echo &quot;<option>Select database</option>\n&quot;;

while ($row = mysql_fetch_row($db_list)) {

if($row[0] == $db){

echo&quot;<option value=$row[0] selected>$row[0]</option>\n&quot;;

}else{

echo&quot;<option value=$row[0]>$row[0]</option>\n&quot;;
}

}

mysql_free_result($db_list);

echo &quot;<input type=submit name=setdb value=\&quot;use database\&quot;></select>\n</form>\n&quot;;

// unset $table when changing database to avoid running invalid queries

if(isset($setdb)){

unset($table);
}


// find tables if database selected or don't show it

if(isset($db)){

echo &quot;<form name=select_table action=$PHP_SELF>&quot;;

$table_list = mysql_list_tables($db);

echo &quot; <select name=table>\n&quot;;

echo &quot;<option>Select Table</option>\n&quot;;

while ($row = mysql_fetch_row($table_list)) {

if($row[0] == $table){

echo&quot;<option value=$row[0] selected>$row[0]</option>\n&quot;;

}else{

echo&quot;<option value=$row[0]>$row[0]</option>\n&quot;;

}

}

echo &quot;<input type=hidden name=db value=$db>\n&quot;;

echo &quot;<input type=submit value=\&quot;use table\&quot;>\n</select>\n</form>\n&quot;;

}

// finish the title table

echo &quot;</td><td width=33%>\n&quot;;

echo &quot;<div align=center><h2> mySQL Explorer Version 1.0</h2></div>&quot;;

echo &quot;</td><td width=33%>\n&quot;;

printf (&quot;<div align=center><h3>MySQL server version: %s </h3></div><br>&quot;, mysql_get_server_info());

printf (&quot;<div align=center><h3>PHP version: %s </h3></div>&quot;, phpversion());

echo &quot;</td></tr>\n</table>\n&quot;;


// set the bd after selection only

if(isset($db)){

@mysql_select_db($db,$connection) ;

$err_no=mysql_errno();

if ($err_no > 0 ){

echo &quot;<h2> Error:</h1> &quot; . mysql_errno() . &quot;: &quot; . mysql_error() . &quot;<br>&quot;;

exit;

}

}


// get the tables from the database once its been selected

if(isset($table)){

// query stuff - used to get all items from the db but you can limit it here
$fields=&quot;*&quot;;

$sql=&quot;SELECT $fields from $table&quot;;

// run query

$result=@mysql_query($sql,$connection);

$err_no=mysql_errno();

if ($err_no > 0 ){

echo &quot;<h2> Error:</h1> &quot; . mysql_errno() . &quot;: &quot; . mysql_error() . &quot;<br>&quot;;

exit;

}

$columns=@mysql_num_fields($result);


// start the table

echo &quot;<table border=0 cellspacing=1 cellpadding=0>\n&quot;;

// figure out how many columns are in it and do the table to fit.

$res_rows=mysql_num_rows($result);

echo &quot;Displaying <b>$res_rows</b> rows:<br>&quot;;

$row=0;

while ($myrow = @mysql_fetch_array($result)){

$row++;

if($row%2){

$color=&quot;#999999&quot;;

}else{

$color=&quot;#599999&quot;;

}

echo &quot;<tr>&quot;;

for ($i = 0; $i < ($columns); $i++) {

echo &quot;<td bgcolor=$color> $myrow[$i] </td>&quot;;

}

echo &quot;</tr>\n&quot;;

}

echo &quot;</table>&quot;;

}

?>
***************************************
Party on, dudes!
[cannon]
 
hmm...doesn't work for me. it lists my database, but when i select it, no tables show up
 
Nice one KarveR! Regards

David Byng

spider.gif


davidbyng@hotmail.com
 
Iostream are you using php4 if so you need to change the variables from $<variable> to $_GET[<variable>] for :
$db becomes $_GET[db]
$setdb = $_GET[setdb]
and $table - $_GET
***************************************
Party on, dudes!
[cannon]
 
yeah, my hosted server uses 4. i'll try that out
 
still didn't work...got errors now. was it just those 3 variables or all variables?
 
Heres the modded one I use at work (PHP4 > 4.0.6)

<!DOCTYPE HTML PUBLIC &quot;-//W3C//DTD HTML 4.0 Transitional//EN&quot;>
<html>
<head>
<title>mySQL Explorer - Version: 1.0</title>
</head>
<body topmargin=&quot;0&quot; leftmargin=&quot;0&quot; marginheight=&quot;0&quot; marginwidth=&quot;0&quot;
bgcolor=&quot;#ffffff&quot; text=&quot;#000000&quot; link=&quot;#000099&quot; alink=&quot;#0000ff&quot;
vlink=&quot;#000099&quot;>

<?php
// check the contents to make sure I get em all
//print_r($_GET);

//////////////////////////////////////////////////////////////////////////////////
// //
// mySQL Explorer version 1 Copyright Karv@virtualkev.com //
// //
// Written : 16th June 2002 //
// Last modified: never //
// Bugs : none it seems (yet) //
// //
//////////////////////////////////////////////////////////////////////////////////


// Database connection details - CHANGE THESE AS NECESSARY //

$host=&quot;localhost&quot;;
$user=&quot;user&quot;;
$password=&quot;password&quot;;

//////////////////////////////////////////////////////////////////////////////////
// You should not need to edit anything below here unless you use PHP Version > 4.0.6
// and are experiencing problems. If so please email me details or problems to fix.
//////////////////////////////////////////////////////////////////////////////////

// connect

$connection=@mysql_connect($host,$user,$password) ;

$err_no=mysql_errno();

if ($err_no > 0 ){

echo &quot;<h2> Error:</h1> &quot; . mysql_errno() . &quot;: &quot; . mysql_error() . &quot;<br>&quot;;

exit;

}

// start the page

echo &quot;<table border=0 width=100% cellspacing=1 cellpadding=0>\n&quot;;

echo &quot;<tr bgcolor=#0099cc>\n<td wdith=34%>\n&quot;;


// find databases;

echo &quot;<form name=select_db action=$_GET[PHP_SELF]>\n&quot;;

$db_list = mysql_list_dbs($connection);

echo &quot; <select name=db>\n&quot;;

echo &quot;<option>Select database</option>\n&quot;;

while ($row = mysql_fetch_row($db_list)) {

if($row[0] == $_GET[db]){

echo&quot;<option value=$row[0] selected>$row[0]</option>\n&quot;;

}else{

echo&quot;<option value=$row[0]>$row[0]</option>\n&quot;;
}

}

mysql_free_result($db_list);

echo &quot;<input type=submit name=setdb value=\&quot;use database\&quot;></select>\n</form>\n&quot;;

// unset $_GET
when changing database to avoid running invalid queries

if(isset($_GET[setdb])){

unset($_GET
);
}


// find tables if database selected or don't show it

if(isset($_GET[db])){

echo &quot;<form name=select_table action=$_GET[PHP_SELF]>&quot;;

$table_list = mysql_list_tables($_GET[db]);

echo &quot; <select name=table>\n&quot;;

echo &quot;<option>Select Table</option>\n&quot;;

while ($row = mysql_fetch_row($table_list)) {

if($row[0] == $_GET
){

echo&quot;<option value=$row[0] selected>$row[0]</option>\n&quot;;

}else{

echo&quot;<option value=$row[0]>$row[0]</option>\n&quot;;

}

}

echo &quot;<input type=hidden name=db value=$_GET[db]>\n&quot;;

echo &quot;<input type=submit value=\&quot;use table\&quot;>\n</select>\n</form>\n&quot;;

}

// finish the title table

echo &quot;</td><td width=33%>\n&quot;;

echo &quot;<div align=center><h2> mySQL Explorer Version 1.0</h2></div>&quot;;

echo &quot;</td><td width=33%>\n&quot;;

printf (&quot;<div align=center><h3>MySQL server version: %s </h3></div><br>&quot;, mysql_get_server_info());

printf (&quot;<div align=center><h3>PHP version: %s </h3></div>&quot;, phpversion());

echo &quot;</td></tr>\n</table>\n&quot;;


// set the bd after selection only

if(isset($_GET[db])){

@mysql_select_db($_GET[db],$connection) ;

$err_no=mysql_errno();

if ($err_no > 0 ){

echo &quot;<h2> Error:</h1> &quot; . mysql_errno() . &quot;: &quot; . mysql_error() . &quot;<br>&quot;;

exit;

}

}


// get the tables from the database once its been selected

if(isset($_GET
)){

// query stuff - used to get all items from the db but you can limit it here
$fields=&quot;*&quot;;

$sql=&quot;SELECT $fields from $_GET
&quot;;

// run query

$result=@mysql_query($sql,$connection);

$err_no=mysql_errno();

if ($err_no > 0 ){

echo &quot;<h2> Error:</h1> &quot; . mysql_errno() . &quot;: &quot; . mysql_error() . &quot;<br>&quot;;

exit;

}

$columns=@mysql_num_fields($result);


// start the table

echo &quot;<table border=0 cellspacing=1 cellpadding=0>\n&quot;;

// figure out how many columns are in it and do the table to fit.

$res_rows=mysql_num_rows($result);

echo &quot;Displaying <b>$res_rows</b> rows:<br>&quot;;

$row=0;

while ($myrow = @mysql_fetch_array($result)){

$row++;

if($row%2){

$color=&quot;#999999&quot;;

}else{

$color=&quot;#599999&quot;;

}

echo &quot;<tr>&quot;;

for ($i = 0; $i < ($columns); $i++) {

echo &quot;<td bgcolor=$color> $myrow[$i] </td>&quot;;

}

echo &quot;</tr>\n&quot;;

}

echo &quot;</table>&quot;;

}

?> ***************************************
Party on, dudes!
[cannon]
 
Modded the code to be able to handle any version of PHP. ( I think :)).

Get it from here
[lol] ***************************************
Party on, dudes!
[cannon]
 
cool, that edited one worked
 
Hey bud,

The modified one from here is working for me

MySQL server version: 3.23.49a
PHP version: 4.2.1

Although I had to move the </select> tags on lines 83 and 123 up so the buttons wern't inside the select list otherwise they don't show on Mozilla 1.0.

Cheers for that, I'm sure it will come in handy sometime. [smurf]
01101000011000010110010001110011
 
Hads, I don't know anything about Mozilla, can you send me / post the mod so I can add it in, thanks.

Also version 2.0 has been let loose (yeah I need to fix the mozilla issue) :).

(Porbably have nutscape issues too).

Added some stuff to allow simple queries and limiting of result sets for large databases.

Check out for info/download. ______________________________________________________________________
There's no present like the time, they say. - Henry's Cat.
 
Hi CarveR!

I tried to download ur Mysql-explorer at: destination: a href=download.php?count=7&counter=mysqlexplorer2.1.zip.txt&filename=mysqlexplorer2.1.zip )
ur zip file contains only the zip-extension at my end, and not ur 3,542 bytes of scripting
:) Kujahn
 
My site was taken to pieces for hosting some dubious UFO footage, I think its all back and working now. :)

______________________________________________________________________
There's no present like the time, they say. - Henry's Cat.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top