gpt4 book ai didi

php mysql 无限循环

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

我有以下代码,除非我注释 *$conn->query($insert_statement.$values);*,否则它会保持在恒定循环中。通过注释该行,我得到了完全正确的打印输出,因为它将在最后一个插入语句处完成循环。

for($count =0; $count < $rs->num_rows; $count++){
$values = "";
foreach($keys as $key){
$values = $values . "'".$row->$key."', ";
}
$values = substr($values, 0, strlen($values)-2) . ");\n";
echo $insert_statement.$values."\n";
$conn->query($insert_statement.$values);
}

这是完整的代码。我正在尝试制作一个脚本来备份表。

<?

$date = getdate();

$date = $date['month']."_".$date['mday']."_".$date['year'];



$conn = new mysqli("localhost", "root", "ranger", "customers");

$conn->query("drop table if exists backup_$date");
$rs = $conn->query("DESCRIBE customers");

$create_statment = "create table backup_$date (";

$primary_key = "";
$keys = array();
$insert_statement = "insert into backup_$date (";

$count = 0;
while($row = $rs->fetch_object()) {
if($count > 0){
$keys[] = "".$row->Field."";
$insert_statement = $insert_statement . "`".$row->Field."` ,";
}
if($row->Null == "NO"){
$create_statment = $create_statment . "`".$row->Field."` ".$row->Type." not null";
if($row->Extra == "auto_increment"){
$create_statment = $create_statment ." not null auto_increment, ";
}else{
$create_statment = $create_statment . ",";
}
}else{
$create_statment = $create_statment . " `".$row->Field."` ".$row->Type.", ";
}
if($row->Key == 'PRI'){
$primary_key = "primary key(".$row->Field.")";
}
$count++;
}
$create_statment = $create_statment . " $primary_key);";

$conn->query($create_statment);





$rs = $conn->query("select * from customers;");

$insert_statement = substr($insert_statement, 0, strlen($insert_statement)-1).") values (";
$row = $rs->fetch_object();
for($count =0; $count < $rs->num_rows; $count++){
$values = "";
foreach($keys as $key){
$values = $values . "'".$row->$key."', ";
}
$values = substr($values, 0, strlen($values)-2) . ");\n";
echo $insert_statement.$values."\n";
$conn->query($insert_statement.$values);
}



?>

最佳答案

我使用 INSERT ... SELECT 创建备份。也许这对你有帮助。

关于php mysql 无限循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14188543/

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