2015-05-14 29 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

谢谢......忘了那个。其始终存在的小错误。 –

相关问题