Showing posts with label MySql. Show all posts
Showing posts with label MySql. Show all posts

Wednesday, April 1, 2009

MySql Table to HTML Table

Hi to all!!

happy april :p like CADIE?

Well now i want to exctract a little preview form dbi that should be useful to someone.

This is a php function and could needed to be a little change from you. in_table function print out a HTML table showing the results of a SELECT query. Actually the function (as it is) need an existent mysql connection and the function mysql_select_db() already called. Then you could add classes to HTML Table element to style as you want.

All the game is based on mysql_fetch_assoc() and array_keys(). It first execute the query (without security check =P) then while cycle result with mysql_fetch_assoc(); then check an internal counter with a value as modulus to "decide" if print headers again or not (headers is the field name or their aliases).
Here the function:

function in_table($query, $repeat = 50) {
$result = mysql_query($query);
if(!$result) {
echo "Error: ".mysql_error();
die();
}
$counter = 0;
echo "<table>";
while($data = mysql_fetch_assoc($result)) {
echo "<tr>";
if($counter % $repeat == 0) {
$fields = array_keys($data);
foreach($fields as $field)
echo "<th>$field</th>";
echo "</tr><tr>";
$counter = 0;
}
foreach($data as $val)
echo "<td>$val</td>";
echo "</tr>";
$counter++;
}
echo "</table>";
}


Here a little snapshoot about the output ^^

Tuesday, March 10, 2009

MySql Database Inspector

Well, came back,
im very busy lately so my posts as you can see, decrease in number. Now i want to start in a new project (lol added to the lot i've already started). It's about mysql database, a simple web application that only show all databases structure in a web browser.

You need to download jquery to let it works.

The workflow is easy: it first put in a multidimensional php array all the information
array[dbs_name][tables_name][fields_name] = field_type.
then create div inside div to graphically render the structure, helped by jquery and putting information out with php.

In the future should be interesting improving it by adding a query box that helps the user in building the query by selecting the type of the query and filling it by clicking on the graphically structure, and obviously manually filling too.

Here the code:
"db_struct.php"
This file contains the function get_structure($mysql_connection_resource) which convert the databases into the array.

function get_structure($conn_res) {
$resdbs = null;
$restbls = null;
$resclmns = null;
$resdbs = mysql_list_dbs($conn_res);
$structure = array();

while($dbs = mysql_fetch_object($resdbs)) {
$dn = $dbs->Database;
$restbls = mysql_list_tables($dn, $conn_res);
while($tbls = mysql_fetch_array($restbls)) {
$tn = $tbls[0];
$resclmns = mysql_list_fields($dn, $tn, $conn_res);
$result = mysql_query("SHOW COLUMNS FROM $tn");
if (mysql_num_rows($result) > 0) {
while ($row = mysql_fetch_assoc($result)) {
$fn = $row['Field'];
$structure["$dn"]["$tn"]["$fn"] = $row["Type"];
}
}
}
}
return $structure;
}


"web gui"
This file is the web render. You need to put your connection data and a link to jquery (i used 2.6)
<?
require_once "db_struct.php";
$conn = mysql_connect("yourhost", "youruser", "yourpass");
$s = get_structure($conn);
?>
<!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">
<head>
<script type="text/javascript" src="jquery_source"></script>
<style>
#main {
width: auto;
}
.database {
float: left;
border: 1px solid darkred;
background: #CCCCCC;
margin: 5px;
padding: 2px;
width: 300px;
overflow: auto;
}
.table {
border: 1px solid darkgreen;
background: #DDDDDD;
margin-bottom: 4px;
padding: 3px;
width: 280px;
overflow: auto;
}
.field {
border: 1px solid darkblue;
background: #EEEEEE;
margin-bottom: 2px;
padding: 2px;
width: 260px;
overflow: auto;
}
span {
font-weight: bold;
}
span.Db {
color: darkred;
font-size: 150%;
}
span.Tb {
color: darkgreen;
font-size: 130%;
}
</style>
</head>
<body>
<div class="closer">
<p>Collapse: </p>
<button id="cdb">All DBS</button>
<button id="ctb">All TBS</button>
<button id="cfd">All FDS</button>
</div>
<div id="main"></div>
<script type="text/javascript">
var main = $("#main");
<? foreach($s as $db => $tbls): ?>
var td = $("<div>");
td.html("<span class='Tit Db'><?= $db ?></span>");
td.addClass("database");
<? foreach($tbls as $tbl => $flds): ?>
var tt = $("<div>");
tt.html("<span class='Tit Tb'><?= $tbl ?></span>");
tt.addClass("table");
<? foreach($flds as $fld => $type): ?>
var tf = $("<div>");
tf.html("<i><?= $fld ?></i> : <?= $type ?>");
tf.addClass("field");
tt.append(tf);
<? endforeach; ?>
td.append(tt);
<? endforeach; ?>
main.append(td);
<? endforeach; ?>
$("#cdb").click(function() {
$(".database").toggle();
});
$("#ctb").click(function() {
$(".table").toggle();
});
$("#cfd").click(function() {
$(".field").toggle();
});
</script>
</body>
</html>
<? mysql_close($conn); ?>

Wednesday, October 8, 2008

A little help

Im gonna show you a simple function in php that will help you to generate the easiest insert mysql query (yet another...).

I use it in a personal web application, someone could find it useful too. It's just an alpha definition, but i really doubt to bring new modifications.

Context:

This function have been called in a asynchronous way from javascript (by XHR), so the parameters comes from $_GET array.

The function accept these parameters as input:

  • $table: name of the table the query concern;
  • $get_unset: ignored $_GET array's indexes that don't need to build the query;
  • $auto_id: this boolean flag is set to true if in that table exist an index called id which is autoincrement

It is clear the follow adaptations should be take into consideration: add an array parameter as a substitute of $_GET array, so $unset_get could be remove from the parameters list. 

Here the code:

function generate_query($table, $get_unset = null, $auto_id = true) {
  $field = "";
  $value = "";
  foreach($get_unset as $gu) unset($_GET[$gu]);
  $query = "INSERT INTO $table(";
  foreach($_GET as $k => $v) {
    $field .= "$k, ";
    $value .= "'$v', ";
  }
  if($auto_id) $query .= "id, ";
  $query .= substr($field, 0, -1).") VALUES(";
  if($auto_id) $query.="null, ";
  $query .= substr($value, 0, -1).")";
  return $query;
}