gpt4 book ai didi

php - 表不存在 mysql 但存在 for 循环

转载 作者:行者123 更新时间:2023-11-29 22:26:26 25 4
gpt4 key购买 nike

我在 mysql 数据库和 php 方面遇到了奇怪的问题。

在 php 中,它获取表名,然后循环遍历表。获取行,然后将变量发送到函数。我正在使用 for 循环来执行此操作。

问题是我有 7 张 table 。如果表 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
}

?>

最佳答案

问题是与数据库的连接。在第一个表发现您的连接已被内部连接更改。因此使用不同的变量进行连接。我正在设置一个运行完美的代码。我已经测试过。

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

}
?>

关于php - 表不存在 mysql 但存在 for 循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30233474/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com