更新MySQL中的所有记录
我目前有一个 17 列宽、有 30 条记录的表。
基本上,它是我从另一个站点抓取的表,然后将其插入到 MySQL 表中。
$html = str_get_html($newHTML); // get the HTML
$tdContents = ""; // declare variable
$rowArray = array(); // declare array for records
for ($j = 0; $j < 510; $j++) // loop through each TD element, 17 columns by 30 records so 510
{
$f = $html->find("td",$j); // get the td elements from the html
$tdContents = $f->innertext; // get the text inside the td
$rowArray[] = $tdContents; // store that text inside the array
if ($j == 16 || $j == 33 || $j == 50 || $j == 67 || $j == 84 || $j == 101 || $j == 118 || $j == 135 || $j == 152 || $j == 169 || $j == 186 || $j == 203 || $j == 220 || $j == 237 || $j == 254 || $j == 271 || $j == 288 || $j == 305 || $j == 322 || $j == 339 || $j == 356 || $j == 373 || $j == 390 || $j == 407 || $j == 424 || $j == 441 || $j == 458 || $j == 475 || $j == 492 || $j == 509) // every 17 td elements
{
$comma_separated = implode("','", $rowArray); // seperate the array contents with commas and apostrophes, set up for mysql
$comma_separated = "'" . $comma_separated . "'"; // add apostrophes to beginning and end of the string
$result = mysql_query("INSERT INTO standings_20112012 VALUES (".$comma_separated.")"); // insert the data into mysql
$rowArray = array(); // clear the array, for the next record
}
}
评论应该解释得很好。这会生成一个 17 x 30 的表格,其中包含我需要的所有信息。如果我再次运行它,它会插入另外 30 条记录,这很糟糕。我只想更新/覆盖等已创建的表。因此表中应该只有 30 条记录。然而我对此感到困惑。
有什么帮助吗?
I currently have a table that is 17 columns wide and has 30 records.
Basically, it's a table from another site that I'm scraping and then inserting into MySQL table.
$html = str_get_html($newHTML); // get the HTML
$tdContents = ""; // declare variable
$rowArray = array(); // declare array for records
for ($j = 0; $j < 510; $j++) // loop through each TD element, 17 columns by 30 records so 510
{
$f = $html->find("td",$j); // get the td elements from the html
$tdContents = $f->innertext; // get the text inside the td
$rowArray[] = $tdContents; // store that text inside the array
if ($j == 16 || $j == 33 || $j == 50 || $j == 67 || $j == 84 || $j == 101 || $j == 118 || $j == 135 || $j == 152 || $j == 169 || $j == 186 || $j == 203 || $j == 220 || $j == 237 || $j == 254 || $j == 271 || $j == 288 || $j == 305 || $j == 322 || $j == 339 || $j == 356 || $j == 373 || $j == 390 || $j == 407 || $j == 424 || $j == 441 || $j == 458 || $j == 475 || $j == 492 || $j == 509) // every 17 td elements
{
$comma_separated = implode("','", $rowArray); // seperate the array contents with commas and apostrophes, set up for mysql
$comma_separated = "'" . $comma_separated . "'"; // add apostrophes to beginning and end of the string
$result = mysql_query("INSERT INTO standings_20112012 VALUES (".$comma_separated.")"); // insert the data into mysql
$rowArray = array(); // clear the array, for the next record
}
}
Comments should explain fine. This generates a table 17 x 30 with all info I need. If I run this again, it'll insert another 30 records, which is bad. I want to only update/overwrite etc. the table that's already created. So there should only ever be 30 records in the table. I'm stumped with this however.
Any help?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
向数据集添加唯一的主键。然后将 mysql 查询更改为 INSERT IGNORE ,它将抛出重复项。或者,如果您需要更新表中已有的内容,也可以使用
ON DUPLICATE UPDATE
。Add a primary key to your dataset that is unique. Then change your mysql query to
INSERT IGNORE
and it will throw out the duplicates. Alternatively you can can also use aON DUPLICATE UPDATE
if you need to update what you already have in the table.