====== 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. * * All rights reserved. * * Redistribution and use in source and binary forms, with or without modification, * are permitted provided that the following conditions are met: * * * Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * * * Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * * * Neither the name of Andreas Gohr nor the names of other contributors may * be used to endorse or promote products derived from this software without * specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ // disable magic quotes if (get_magic_quotes_gpc() && !defined('MAGIC_QUOTES_STRIPPED')){ $_POST = array_map('stripslashes',$_POST); } if(!$_POST['db_host']) $_POST['db_host'] = 'localhost'; ?> MySimple
Database Connection
Query
(separate multiple queries with a semicolon at end of line)
Results'; // connect to db host $link = @mysql_connect($_POST['db_host'], $_POST['db_user'], $_POST['db_pass']); if(!$link){ echo "Could not connect: ".mysql_error()."
"; $ok = false; }else{ echo "Connected to host
"; } // select database if($ok){ if($_POST['db_name']){ if(!@mysql_select_db($_POST['db_name'])){ echo "Could not select DB: ".mysql_error()."
"; $ok = false; }else{ echo "Database selected
"; } } } // run queries if($ok){ if($_POST['query']){ $queries = preg_split("/;(\r\n|\r|\n)/s",$_POST['query']); $queries = array_filter($queries); foreach($queries as $query){ echo '
'; $result = @mysql_query($query); if(!$result){ echo "Query failed: ".mysql_error()."
".htmlspecialchars($query)."

"; }else{ echo ''.mysql_affected_rows($link).' affected rows
'; if($result != 1){ echo ''."\n"; $first = true; while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) { if($first){ echo "\t\n"; foreach (array_keys($line) as $col_value) { echo "\t\t\n"; } echo "\t\n"; $first = false; } echo "\t\n"; foreach ($line as $col_value) { echo "\t\t\n"; } echo "\t\n"; } echo "
".htmlspecialchars($col_value)."
".htmlspecialchars($col_value)."
\n"; } } } } } echo ''; } ?>