php mysql无限循环

时间:2013-01-07 01:04:50

标签: php mysql loops

除非我评论* $ 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);
}



?>

1 个答案:

答案 0 :(得分:1)

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