Posted By

localhorst on 03/15/08


Tagged

mysql simple admin phpmyadmin


Versions (?)

Who likes this?

6 people have marked this snippet as a favorite

iTony
drwitt
wizard04
PapTom
umang_nine
clac


MySimple.php


 / Published in: PHP
 

URL: http://wiki.splitbrain.org/mysimple.php

This simple script can be used to send any query to a MySQL database - useful if you need to quickly access a database without install a full blown manager like PHPMyAdmin.

  1. <?php
  2. /**
  3.  * Simple PHP MySQL Client
  4.  * Copyright (c) 2007, Andreas Gohr <andi (at) splitbrain.org>
  5.  *
  6.  * All rights reserved.
  7.  *
  8.  * Redistribution and use in source and binary forms, with or without modification,
  9.  * are permitted provided that the following conditions are met:
  10.  *
  11.  * * Redistributions of source code must retain the above copyright notice,
  12.  * this list of conditions and the following disclaimer.
  13.  *
  14.  * * Redistributions in binary form must reproduce the above copyright notice,
  15.  * this list of conditions and the following disclaimer in the documentation
  16.  * and/or other materials provided with the distribution.
  17.  *
  18.  * * Neither the name of Andreas Gohr nor the names of other contributors may
  19.  * be used to endorse or promote products derived from this software without
  20.  * specific prior written permission.
  21.  *
  22.  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  23.  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  24.  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  25.  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  26.  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  27.  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  28.  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  29.  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  30.  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  31.  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  32.  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  33.  */
  34.  
  35.  
  36. // disable magic quotes
  37. if (get_magic_quotes_gpc() && !defined('MAGIC_QUOTES_STRIPPED')){
  38. $_POST = array_map('stripslashes',$_POST);
  39. }
  40.  
  41. if(!$_POST['db_host']) $_POST['db_host'] = 'localhost';
  42. ?>
  43. <html>
  44. <head>
  45. <title>MySimple</title>
  46. <style type="text/css">
  47. body {
  48. font: 90% sans-serif;
  49. }
  50.  
  51. table {
  52. font: 90% sans-serif;
  53. border-right: 1px solid #000;
  54. border-top: 1px solid #000;
  55. empty-cells: show;
  56. }
  57. td, th {
  58. border-left: 1px solid #000;
  59. border-bottom: 1px solid #000;
  60. padding: 0.2em 0.5em;
  61. }
  62. input, textarea, fieldset {
  63. border: 1px solid #333;
  64. }
  65. </style>
  66. </head>
  67. <body>
  68. <form action="" method="post">
  69. <fieldset>
  70. <legend>Database Connection</legend>
  71.  
  72. <label for="db_host">Host:</label>
  73. <input type="text" name="db_user" value="<?php echo htmlspecialchars($_POST['db_host'])?>" />
  74.  
  75. <label for="db_name">Database:</label>
  76. <input type="text" name="db_name" value="<?php echo htmlspecialchars($_POST['db_name'])?>" />
  77.  
  78. <label for="db_user">User:</label>
  79. <input type="text" name="db_user" value="<?php echo htmlspecialchars($_POST['db_user'])?>" />
  80.  
  81. <label for="db_pass">Password:</label>
  82. <input type="password" name="db_pass" value="<?php echo htmlspecialchars($_POST['db_pass'])?>" />
  83. </fieldset>
  84.  
  85. <fieldset>
  86. <legend>Query</legend>
  87. <textarea name="query" style="width: 98%" rows="20"><?php echo htmlspecialchars($_POST['query'])?></textarea><br />
  88.  
  89. <input type="submit" name="go" style="cursor: hand" /> (separate multiple queries with a semicolon at end of line)
  90. </fieldset>
  91.  
  92. <?php
  93. // do the work
  94. if($_POST['go']){
  95. $ok = true;
  96. echo '<fieldset><legend>Results</legend>';
  97.  
  98. // connect to db host
  99. $link = @mysql_connect($_POST['db_host'], $_POST['db_user'], $_POST['db_pass']);
  100. if(!$link){
  101. echo "<b>Could not connect: ".mysql_error()."</b><br />";
  102. $ok = false;
  103. }else{
  104. echo "<b>Connected to host</b><br />";
  105. }
  106.  
  107. // select database
  108. if($ok){
  109. if($_POST['db_name']){
  110. if(!@mysql_select_db($_POST['db_name'])){
  111. echo "<b>Could not select DB: ".mysql_error()."</b><br />";
  112. $ok = false;
  113. }else{
  114. echo "<b>Database selected</b><br />";
  115. }
  116. }
  117. }
  118.  
  119. // run queries
  120. if($ok){
  121. if($_POST['query']){
  122. $queries = preg_split("/;(
  123. |\r|\n)/s",$_POST['query']);
  124. $queries = array_filter($queries);
  125.  
  126. foreach($queries as $query){
  127. echo '<hr >';
  128. $result = @mysql_query($query);
  129. if(!$result){
  130. echo "<b>Query failed: ".mysql_error()."</b><br /><pre>".htmlspecialchars($query)."</pre><br />";
  131. }else{
  132. echo '<b>'.mysql_affected_rows($link).' affected rows</b><br />';
  133.  
  134. if($result != 1){
  135. echo '<table cellpadding="0" cellspacing="0">'."\n";
  136. $first = true;
  137. while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
  138. if($first){
  139. echo "\t<tr>\n";
  140. foreach (array_keys($line) as $col_value) {
  141. echo "\t\t<th>".htmlspecialchars($col_value)."</th>\n";
  142. }
  143. echo "\t</tr>\n";
  144. $first = false;
  145. }
  146. echo "\t<tr>\n";
  147. foreach ($line as $col_value) {
  148. echo "\t\t<td>".htmlspecialchars($col_value)."</td>\n";
  149. }
  150. echo "\t</tr>\n";
  151. }
  152. echo "</table>\n";
  153. }
  154. }
  155. }
  156. }
  157. }
  158.  
  159. echo '</pre></fieldset>';
  160. }
  161. ?>
  162. </form>
  163. </body>
  164. </html>

Report this snippet  

Comments

RSS Icon Subscribe to comments
Posted By: PapTom on February 11, 2009

That is a great code! Cheers!

Posted By: refaelgold on December 8, 2012

very nice.Love it!!:)

You need to login to post a comment.