Guest User

PHP code - MYSQL

a guest
Aug 27th, 2013
246
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.88 KB | None | 0 0
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
  2.     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
  3. <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
  4. <head>
  5. <title>MySQL Connection Test</title>
  6. <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
  7. <style type="text/css">
  8. #wrapper {
  9.     width: 600px;
  10.     margin: 20px auto 0;
  11.     font: 1.2em Verdana, Arial, sans-serif;
  12. }
  13. input {
  14.     font-size: 1em;
  15. }
  16. #submit {
  17.     padding: 4px 8px;
  18. }
  19. </style>
  20. </head>
  21.  
  22. <body>
  23.  
  24. <div id="wrapper">
  25.  
  26. <?php
  27.     $action = htmlspecialchars($_GET['action'], ENT_QUOTES);
  28. ?>
  29.  
  30. <?php if (!$action) { ?>
  31.  
  32.     <h1>MySQL connection test</h1>
  33.  
  34. <form action="<?php echo $_SERVER['PHP_SELF']; ?>?action=test" id="mail" method="post">
  35.  
  36.     <table cellpadding="2">
  37.         <tr>
  38.             <td>Hostname</td>
  39.             <td><input type="text" name="hostname" id="hostname" value="" size="30" tabindex="1" /></td>
  40.             <td>(usually "localhost")</td>
  41.         </tr>
  42.         <tr>
  43.             <td>Username</td>
  44.             <td><input type="text" name="username" id="username" value="" size="30" tabindex="2" /></td>
  45.             <td></td>
  46.         </tr>
  47.         <tr>
  48.             <td>Password</td>
  49.             <td><input type="text" name="password" id="password" value="" size="30" tabindex="3" /></td>
  50.             <td></td>
  51.         </tr>
  52.         <tr>
  53.             <td>Database</td>
  54.             <td><input type="text" name="database" id="database" value="" size="30" tabindex="4" /></td>
  55.             <td>(optional)</td>
  56.         </tr>
  57.         <tr>
  58.             <td></td>
  59.             <td><input type="submit" id="submit" value="Test Connection" tabindex="5" /></td>
  60.             <td></td>
  61.         </tr>
  62.     </table>
  63.  
  64. </form>
  65.  
  66. <?php } ?>
  67.  
  68. <?php if ($action == "test") {
  69.  
  70. // The variables have not been adequately sanitized to protect against SQL Injection attacks: http://us3.php.net/mysql_real_escape_string
  71.  
  72.     $hostname = trim($_POST['hostname']);
  73.     $username = trim($_POST['username']);
  74.     $password = trim($_POST['password']);
  75.     $database = trim($_POST['database']);
  76.  
  77.     $link = mysql_connect("$hostname", "$username", "$password");
  78.         if (!$link) {
  79.             echo "<p>Could not connect to the server '" . $hostname . "'</p>n";
  80.             echo mysql_error();
  81.         }else{
  82.             echo "<p>Successfully connected to the server '" . $hostname . "'</p>n";
  83. //          printf("MySQL client info: %sn", mysql_get_client_info());
  84. //          printf("MySQL host info: %sn", mysql_get_host_info());
  85. //          printf("MySQL server version: %sn", mysql_get_server_info());
  86. //          printf("MySQL protocol version: %sn", mysql_get_proto_info());
  87.         }
  88.     if ($link && !$database) {
  89.         echo "<p>No database name was given. Available databases:</p>n";
  90.         $db_list = mysql_list_dbs($link);
  91.         echo "<pre>n";
  92.         while ($row = mysql_fetch_array($db_list)) {
  93.             echo $row['Database'] . "n";
  94.         }
  95.         echo "</pre>n";
  96.     }
  97.     if ($database) {
  98.     $dbcheck = mysql_select_db("$database");
  99.         if (!$dbcheck) {
  100.             echo mysql_error();
  101.         }else{
  102.             echo "<p>Successfully connected to the database '" . $database . "'</p>n";
  103.             // Check tables
  104.             $sql = "SHOW TABLES FROM `$database`";
  105.             $result = mysql_query($sql);
  106.             if (mysql_num_rows($result) > 0) {
  107.                 echo "<p>Available tables:</p>n";
  108.                 echo "<pre>n";
  109.                 while ($row = mysql_fetch_row($result)) {
  110.                     echo "{$row[0]}n";
  111.                 }
  112.                 echo "</pre>n";
  113.             } else {
  114.                 echo "<p>The database '" . $database . "' contains no tables.</p>n";
  115.                 echo mysql_error();
  116.             }
  117.         }
  118.     }
  119. }
  120. ?>
  121.  
  122. </div><!-- end #wrapper -->
  123. </body>
  124. </html>
Advertisement
Add Comment
Please, Sign In to add comment