简体   繁体   English

使用一些其他数据将1个sql表复制到另一个

[英]Copy 1 sql table to another with some additional data

I have this code: 我有以下代码:

<?php
//MYSQL
$dbserver="..."; //adresa MySQL
$dblogin="...";       //jméno uživatele MySQL
$dbheslo="...";     //heslo MySQL
$dbnazev="...";      //název databáze MySQL
$mysqli = new mysqli($dbserver, $dblogin, $dbheslo, $dbnazev); 
if ($mysqli->connect_error) {
    die('Nepodařilo se připojit k MySQL serveru (' . $mysqli->connect_errno . ') '
            . $mysqli->connect_error);
}
$mysqli->query("SET NAMES 'utf8'"); // nastavíme kódování MySQL



while($row = $mysqli->query("SELECT * FROM `importovat_fyz` WHERE `tempid` IS NOT NULL")->fetch_assoc()){

$tempid = $row['tempid'];
$jmeno = $row['jmeno'];
$prijmeni = $row['prijmeni'];


$email = $row['email'];
$bydliste = $row['bydliste'];

$souhlas = "on";
$aktuality = "on";

$timestamp = time();
$hash = md5("77c0a83besxxxcg1a190ab90d".time().$tempid.rand(10000000, 99999999));

$poznamky = "import19052018";

$vloz ="INSERT into `potvrzenotest` set jmeno='".$jmeno."', prijmeni='".$prijmeni."', bydliste='".$bydliste."', email='".$email."', souhlas='".$souhlas."', aktuality='".$aktuality."', timestamp='".$timestamp."', hash='".$hash."', poznamky='".$poznamky."';";
$result=$mysqli->query($vloz);


$cas = date('H:i');
echo '['.$cas.'] ID '.$tempid.' imported.', PHP_EOL;
}

mysqli_close($mysqli); .
?>

I have one table with some data and I have to copy it to another table with some additional data (like hash etc.). 我有一个包含一些数据的表,我必须将它复制到具有一些其他数据(如哈希等)的另一张表中。

When I run the code above, I got this: 当我运行上面的代码时,我得到了:

[17:17] ID 1 imported.
[17:17] ID 1 imported.
[17:17] ID 1 imported.
[17:17] ID 1 imported.
[17:17] ID 1 imported.

So, only 1 row is being copied. 因此,仅复制1行。

Could you please help me, where is the problem? 您能帮我吗,问题出在哪里?

while($row = $mysqli->query("SELECT * FROM `importovat_fyz` WHERE `tempid` IS NOT NULL")->fetch_assoc()){ ... }

This loop will never end. 这个循环永远不会结束。 And it will fetch the first row again and again. 并且它将一次又一次地获取第一行。

You should execute the query outside the loop: 您应该在循环外执行查询:

$selectResult = $mysqli->query("SELECT * FROM `importovat_fyz` WHERE `tempid` IS NOT NULL");

while ($row = $selectResult->fetch_assoc()) { ... }

As a side note: Consider to use a prepared statement for your INSERT statement in the loop. 附带说明:考虑在循环中为INSERT语句使用准备好的语句 This will prevent SQL syntax errors if some of the values may contail special characters like ' . 如果某些值可能需要特殊字符(如' ,则可以防止SQL语法错误。 If you run them in one transaction , you might even improve the performance. 如果在一个事务中运行它们,甚至可以提高性能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 SQL - 将数据从一个表列复制到另一个表列 - SQL - copy data from one table column to another table column SQL复制数据从一列到另一个表指定列 - SQL copy data from one column to another table specified column 使用SQL查询和模型Codeigniter将数据复制到另一个表 - Copy data to another table using sql query and model codeigniter MySQL自动将一个数据库表的表数据复制到另一数据库表; 随着表格的更新或按特定的时间间隔,计划 - MySQL Copy Table Data of one Database Table to another AUTOMATICALLY; as table gets updated or by some specific time interval, schedule 制作一个新的sql表,其中一个字段从另一个表字段复制数据 - Making a new sql table, where one of the fields copy data from another table field 将数据从一个 sql 表复制到另一个具有较少列数和不同列的表 - copy data from one sql table to another having less number of columns and a different column 将数据从表复制到另一个没有ID的数据 - Copy data from a table to another without ID 无法将数据从一个表复制到另一个表 - Not able to copy data from one table to another Laravel - 将集合数据复制到另一个表并删除 - Laravel - Copy collection data to another table and delete 将数据从一个表复制到另一个表? - copy data from one table into another?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM