2015-10-09 66 views
0

我有自己託管的Web應用程序,我試圖訪問information_schema表一些如何可以在某些服務器禁用它。如何檢查它是否可訪問?如何檢查一個mysql表可訪問或不通過php

+1

https://ardamis.com/2008/05/26/a-php-script-for-testing-a-mysql -database-connection/ –

+0

謝謝你,夥計,它的工作 – Developer

+1

我發佈下面的答案爲他人。如果它適合你,請將其標記爲正確。謝謝:) –

回答

1

運行SHOW DATABASES並查看information_schema是否在列表中。

但這不應該是正確的?如果你是自主託管,你可以配置它,但你需要它。

+0

Dude'information_schema'與任何數據庫都沒有關係,所以它不會顯示在'show tables'查詢中,是的,我需要知道有多少用戶可以訪問'information_schema' – Developer

+0

如何配置它? – Developer

+0

@Developer我的意思是'SHOW DATABASES'當然> _> – Halcyon

1

將數據庫設置爲information_schema並從選定數據庫運行查詢「顯示錶」。如果你有讀權限,那麼你將從該數據庫中獲取所有表。檢查所有查詢的結果。

+0

該死的真正的傢伙:+1: – Developer

2

樣品溶液解決此問題:

<!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> 
    <title>MySQL Connection Test</title> 
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> 
    <style type="text/css"> 
    #wrapper { 
     width: 600px; 
     margin: 20px auto 0; 
     font: 1.2em Verdana, Arial, sans-serif; 
    } 
    input { 
     font-size: 1em; 
    } 
    #submit { 
     padding: 4px 8px; 
    } 
    </style> 
    </head> 

    <body> 

    <div id="wrapper"> 

    <?php 
     $action = htmlspecialchars($_GET['action'], ENT_QUOTES); 
    ?> 

    <?php if (!$action) { ?> 

     <h1>MySQL connection test</h1> 

    <form action="<?php echo $_SERVER['PHP_SELF']; ?>?action=test" id="mail" method="post"> 

     <table cellpadding="2"> 
      <tr> 
       <td>Hostname</td> 
       <td><input type="text" name="hostname" id="hostname" value="" size="30" tabindex="1" /></td> 
       <td>(usually "localhost")</td> 
      </tr> 
      <tr> 
       <td>Username</td> 
       <td><input type="text" name="username" id="username" value="" size="30" tabindex="2" /></td> 
       <td></td> 
      </tr> 
      <tr> 
       <td>Password</td> 
       <td><input type="text" name="password" id="password" value="" size="30" tabindex="3" /></td> 
       <td></td> 
      </tr> 
      <tr> 
       <td>Database</td> 
       <td><input type="text" name="database" id="database" value="" size="30" tabindex="4" /></td> 
       <td>(optional)</td> 
      </tr> 
      <tr> 
       <td></td> 
       <td><input type="submit" id="submit" value="Test Connection" tabindex="5" /></td> 
       <td></td> 
      </tr> 
     </table> 

    </form> 

    <?php } ?> 

    <?php if ($action == "test") { 

    // The variables have not been adequately sanitized to protect against SQL Injection attacks: http://us3.php.net/mysql_real_escape_string 

     $hostname = trim($_POST['hostname']); 
     $username = trim($_POST['username']); 
     $password = trim($_POST['password']); 
     $database = trim($_POST['database']); 

     $link = mysql_connect("$hostname", "$username", "$password"); 
      if (!$link) { 
       echo "<p>Could not connect to the server '" . $hostname . "'</p>\n"; 
       echo mysql_error(); 
      }else{ 
       echo "<p>Successfully connected to the server '" . $hostname . "'</p>\n"; 
    //   printf("MySQL client info: %s\n", mysql_get_client_info()); 
    //   printf("MySQL host info: %s\n", mysql_get_host_info()); 
    //   printf("MySQL server version: %s\n", mysql_get_server_info()); 
    //   printf("MySQL protocol version: %s\n", mysql_get_proto_info()); 
      } 
     if ($link && !$database) { 
      echo "<p>No database name was given. Available databases:</p>\n"; 
      $db_list = mysql_list_dbs($link); 
      echo "<pre>\n"; 
      while ($row = mysql_fetch_array($db_list)) { 
       echo $row['Database'] . "\n"; 
      } 
      echo "</pre>\n"; 
     } 
     if ($database) { 
     $dbcheck = mysql_select_db("$database"); 
      if (!$dbcheck) { 
       echo mysql_error(); 
      }else{ 
       echo "<p>Successfully connected to the database '" . $database . "'</p>\n"; 
       // Check tables 
       $sql = "SHOW TABLES FROM `$database`"; 
       $result = mysql_query($sql); 
       if (mysql_num_rows($result) > 0) { 
        echo "<p>Available tables:</p>\n"; 
        echo "<pre>\n"; 
        while ($row = mysql_fetch_row($result)) { 
         echo "{$row[0]}\n"; 
        } 
        echo "</pre>\n"; 
       } else { 
        echo "<p>The database '" . $database . "' contains no tables.</p>\n"; 
        echo mysql_error(); 
       } 
      } 
     } 
    } 
    ?> 

    </div><!-- end #wrapper --> 
    </body> 
    </html> 

參考:[https://ardamis.com/2008/05/26/a-php-script-for-testing-a-mysql-database-connection/][1]

+0

它的工作'mysql_list_dbs'幫助我 – Developer

+0

我很高興它幫助你。 –

+0

感謝哥們,快樂編碼! – Developer

相關問題