2015-05-14 60 views
0

我hava在這裏與mysql數據庫和PHP weiring問題。表不存在mysql,但它爲循環

在PHP中,它獲取表名,然後循環遍歷表。獲取行,然後將變量發送給函數。我正在使用循環來做到這一點。

問題是我有7個表。如果表2和表3包含行,則它循環遍歷2並正確執行所有操作。並且它不會給表1中的表不存在錯誤,但是對於表3-7,它給出所有表,因爲表不存在。

但是,如果我刪除表2中的行,然後它循環通過3,並正常工作,並沒有給1和2的錯誤,但對於表4-7它給表不存在錯誤。 基本上它只通過一個表格循環,併爲所有後續表格提供錯誤。我不明白的是,連接問題到MySQL或其他東西。

下面是我的代碼片段:

 <?php 
     $hostname_localhost ="localhost"; 
     $username_localhost ="xxxxxxx"; 
     $password_localhost ="xxxxxxxxx"; 
     $database_xxxxxxxxxx ="xxxxxxxxx"; 
     $database_yyyyyyyyyyy ="yyyyyyyyyy"; 
     $database_zzzzz = "zzzzz"; 

     $localhost = mysql_connect($hostname_localhost,$username_localhost,$password_localhost) 
     or 
     trigger_error(mysql_error(),E_USER_ERROR); 

     $get_all_tables = mysql_query("SHOW TABLES FROM xxxxxxxxx"); 

     $table_names = array(); 

     while($row = mysql_fetch_array($get_all_tables, MYSQL_NUM)) { 
     array_push($table_names,$row[0]); 
     } 

     mysql_select_db($database_xxxxxxx, $localhost); 


     for ($i=0; $i<sizeof($table_names); $i++){ 


      $table = trim($table_names[$i]); 

      $get_tag = mysql_query("SELECT * FROM $table"); 

      if ($get_tag){ 

      while($get_tag_infos = mysql_fetch_array($get_tag)){ 

           $similarity = $get_tag_infos['similarity']; 
           //........... and many other variables 

           if ($similarity == 20){ 

            get20(many variables); 

           } 
           if ($similarity == 40){ 

            get40(many variables); 

           } 
           if ($similarity == 60){ 

            get60(many varibales); 

           } 
           if ($similarity == 80){ 

            get80(many variables); 

           } 
           if ($similarity == 100){ 

            get100(many variables); 

           } 
          } 
      } 
      else{ 
       echo '</br> error! </br>'.mysql_error().mysql_errno(); 
      } 
     } 

     function get20(many variables){ 
      // performs a insert function to mysql 
     } 

     function get40(many variables){ 
      // performs a insert function to mysql 
     } 

     function get60(many variables){ 
      // performs a insert function to mysql 
     } 

     function get80(many variables){ 
      // performs a insert function to mysql 

     } 

     function get100(many variables){ 
      // performs a insert function to mysql 
     } 

     ?> 

回答

0

問題是與數據庫的連接。在第一張桌子找到你的連接後,已經被內部連接改變了。因此使用不同的變量進行連接。我正在設置一個完美運行的代碼。我測試過了。

<?php 
//conection: 
$link = mysqli_connect("localhost","root","pass","test") or die("Error " . mysqli_error($link)); 

//consultation: 

$query = "show tables" or die("Error in the consult.." . mysqli_error($link)); 

//execute the query. 

$result = mysqli_query($link, $query); 

//display information: 

while($row = mysqli_fetch_array($result)) { 
    echo $row["Tables_in_test"] . "<br>"; 

    $link2 = mysqli_connect("localhost","root","pass","test") or die("Error " . mysqli_error($link)); 
    $query2 = "select * from ".$row['Tables_in_test'] or die("Error in the consult.." . mysqli_error($link2)); 
    $result2 = mysqli_query($link2, $query2); 
    while($row2 = mysqli_fetch_array($result2)) { 
    echo "<pre>"; 
    print_r($row2); 
    echo "</pre>"; 
    } 

} 
?> 
+0

謝謝......忘了那個。其始終存在的小錯誤。 –