Skip to content

Instantly share code, notes, and snippets.

@bigprof
Last active May 2, 2018 10:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bigprof/0d11fc733a58064a928698947ae4c933 to your computer and use it in GitHub Desktop.
Save bigprof/0d11fc733a58064a928698947ae4c933 to your computer and use it in GitHub Desktop.
Script to display INSERT/UPDATE/SELECT queries for a specific table in a specific database (you'll be asked for these first). The queries are suitable for use in PHP programs. Don't forget to sanitize any variables before using them in your queries! Here is a short screencast showing it in action: https://www.screencast.com/t/tvyp9P1F
<?php
/* Change the values below to match your database */
$config = [
'server' => 'localhost',
'user' => 'dbuser',
'pass' => 'password'
];
if(!isset($_REQUEST['db']) || !isset($_REQUEST['table'])) {
?>
<form method="post" action="mysql2php.php">
<div class="form-group">
<label for="db" class="control-label">DB Name</label>
<input type="text" class="form-control" id="db" name="db" autofocus>
</div>
<div class="form-group">
<label for="table" class="control-label">Table Name</label>
<input type="text" class="form-control" id="table" name="table">
</div>
<div class="row">
<div class="col-sm-offset-3 col-sm-6">
<button class="btn btn-primary btn-lg btn-block" value="submit" id="submit_button" type="submit" name="submit_button">Submit</button>
</div>
</div>
</form>
<?php
exit;
}
$config['db'] = $_REQUEST['db'];
$config['table'] = $_REQUEST['table'];
$db = @mysqli_connect($config['server'], $config['user'], $config['pass'], $config['db']);
if(!$db) die('Connection error: ' . mysqli_connect_error());
$res = mysqli_query($db, "select * from `{$config['table']}` limit 1");
if($res === false) die('Query error: ' . mysqli_error($db));
$num_fields = mysqli_field_count($db);
$fields = [];
$pk = '';
for($i = 0; $i < $num_fields; $i++) {
mysqli_field_seek($res, $i);
$field = mysqli_fetch_field($res);
$fields[] = $field->name;
if($field->flags & 2) $pk = $field->name;
}
$field_list = $set = $set_arr = $vars = $vars_arr = [];
foreach($fields as $field) {
$field_list[] = "`{$field}`";
$vars[] = "'{\${$field}}'";
$vars_arr[] = "'{\$data['{$field}']}'";
$set[] = "`{$field}`='{\${$field}}'";
$set_arr[] = "`{$field}`='{\$data['{$field}']}'";
}
$statements = [
'INSERT VALUES' => "INSERT INTO `{$config['table']}` (\n\t" .
implode(', ', $field_list) .
"\n) VALUES (\n\t" .
implode(', ', $vars) .
"\n)",
'INSERT VALUES array' => "INSERT INTO `{$config['table']}` (\n\t" .
implode(', ', $field_list) .
"\n) VALUES (\n\t" .
implode(",\n\t", $vars_arr) .
"\n)",
'INSERT SET' => "INSERT INTO `{$config['table']}` SET\n\t" . implode(",\n\t", $set),
'INSERT SET array' => "INSERT INTO `{$config['table']}` SET\n\t" . implode(",\n\t", $set_arr),
'UPDATE' => "UPDATE `{$config['table']}` SET\n\t" . implode(",\n\t", $set) . "\nWHERE `{$pk}`='{\${$pk}}'",
'UPDATE array' => "UPDATE `{$config['table']}` SET\n\t" . implode(",\n\t", $set_arr) . "\nWHERE `{$pk}`='{\$data['{$pk}']}'",
'SELECT' => "SELECT \n\t" . implode(', ', $field_list) . "\nFROM `{$config['table']}` \nWHERE `{$pk}`='{\${$pk}}'"
];
foreach($statements as $title => $st) {
$lines = substr_count($st, "\n") + 4;
?>
<h3><?php echo $title; ?></h3>
<textarea onfocus="this.select();" wrap="off" style="height: <?php echo $lines; ?>em;"><?php echo $st; ?></textarea>
<?php
}
?>
<style>
textarea { width: calc(100% - 4em); margin: 0 2em; }
h3 { margin: 2em 0 0; }
</style>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment