简体   繁体   中英

table does not exists mysql but it does for loop

I hava weired problem here with mysql database and php.

In the php, it gets the table names and then loops through the tables. Takes the rows and then send the variables to a function. I am using for loops to do this.

The problem is that I have 7 tables. If table 2 and 3 contain rows then it loops through 2 and does everything correctly. and it does not give table does not exists error for table 1. but for table 3-7 it gives all as table not exists.

but if I remove rows in table 2 then it loops through 3 and works correctly, and gives no error for 1 and 2. but for table 4-7 it gives table not exists err. Basically it only loops through one table with rows and gives errors for all following tables. I don't understand is that a connection problem to mysql or something else.

Below is the snippet of my code:

        <?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
        }

        ?>

The problem is connection with database. after first table find your connection with has been changed by inner connection. So use different variables for connections. I am setting a code which is running perfectly. I have tested.

<?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>";
  }

} 
?> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM