我有这个代码从一个表中选择数据并在数组中将其移动到另一个表。
<?php
// CHANGE THE CONNECTION INFORMATION TO YOUR DETAILS (OFF YOU CARD)
//$connection = mysql_connect("localhost","root");
$conn = new COM("ADODB.Connection") or die("Cannot start ADO");
$conn->Open("Provider=SQLOLEDB; ..");
$conn2 = new COM("ADODB.Connection") or die("Cannot start ADO");
$conn2->Open("Provider=SQLOLEDB; Data Source=...;
Initial Catalog=...; User ID=...; Password=...");
$rs = $conn->Execute("SELECT ColourID, Width, Height, Price FROM VS_Matrix"); // Recordset
$num_rows = $rs->Fields->Count();
for ($i=0; $i < $num_rows; $i++) {
$fld[$i] = $rs->Fields($i);
$fld2[$i] = $rs->Fields($i);
$fld3[$i] = $rs->Fields($i);
$fld4[$i] = $rs->Fields($i);
$x= 469;
}
$rowcount = 0;
while (!$rs->EOF) {
for ($i=0; $i < $num_rows; $i++) {
echo $fld[$i]->value . "\t";
$rs2 = $conn2->Execute("INSERT INTO TST (ID, ColourID, Width, Height, Price) VALUES ('".$x."','".$fld[$i]."','".$fld2[$i]."','".$fld3[$i]."','".$fld4[$i]."')");
}
echo "\n";
$rowcount++;
$x=$x+1; // increments rowcount
$rs->MoveNext();
}
$rs->Close();
$conn->Close();
$rs = null;
$conn = null;
出于某种原因,它的放置记录似乎随机可能导致我犯了一个愚蠢的错误。有谁能发现它?
答案 0 :(得分:0)
从PHP手册用户评论PHP: COM - Manual:
$rows=0;
while(!$rs->EOF) {
for($i=0;$i<$rs->Fields->count;$i++) {
$rsf = $rs->Fields($i);
if($associative) $rtn[$rows][$rsf->Name] = $rsf->value;
else $rtn[$rows][$i] = $rsf->value;
}
$rows++;
$rs->MoveNext();
}
在此之后,您将$rtn
$rtn[$row]
- 其中一个数组由一行数据库的值组成。
答案 1 :(得分:0)
根据你的问题,我不确定你为什么要做这些额外的工作。假设ID是AUTO_INCREMENT,您可以:
INSERT INTO TST (ColourID, Width, Height, Price)
SELECT ColourID, Width, Height, Price FROM VS_Matrix