php - 限制保存到其他表的数据数量

标签 php mysql

我必须表:

tb_document           tb_sentence
=================     ===========================================
|doc_id|document|     | id | sentence_id |sentence | doc_id|
=================     ===========================================

我想从tb_document获取document数据。数据是文本,然后将数据解析成句子。 我只需要为 tb_sentence 中的每个文档存储最多 50 个句子。

这是代码:

foreach ($content as $doc_id => $sentences){ //$content is variable that save document from tb_document
   $i = 0;
   foreach(preg_split('/\\.\\s*/', $sentences) as $current_sentence){
      $q = mysql_query("INSERT INTO tb_sentence SET doc_id = {$doc_id}, sentence_id = {$i}, sentence = '{$current_sentence}'") or die(mysql_error());
      $i<51; $i++;
   }
}

但是,它仍然保存整个数据。请帮我。谢谢你:)

最佳答案

在您的代码中,您将 $i 与 51 进行比较,但不对比较执行任何操作。尝试:

foreach ($content as $doc_id => $sentences){ //$content is variable that save document from tb_document
   $i = 0;
   foreach(preg_split('/\\.\\s*/', $sentences) as $current_sentence){
      $q = mysql_query("INSERT INTO tb_sentence SET doc_id = {$doc_id}, sentence_id = {$i}, sentence = '{$current_sentence}'") or die(mysql_error());

      if(++$i >= 50) 
          break; 
   }
}

break 语句将从 foreach 循环中退出。

关于php - 限制保存到其他表的数据数量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12803324/

相关文章:

php - Laravel @foreach 在 Blade 中导致错误

php - MySQL - 仅选择许多图像中的一个(随机)图像

php - PHP 如何使用 header() 函数设置 HTTP header ?

php - 是否有可能使用 PHP 获取 Google 网站管理员工具 (GET) 数据?

php - 从不同主机将数据从 Postgresql 传输到 Mysql(从 RoR 数据库到 Wordpress 数据库)

c# - 在 C# 中从 MySql 读取特定行

PHP:未选择数据库

mysql - 如何告诉 Selenium 使用测试数据库?

php - 有效的 MySQL 查询未返回结果

mysql - Mediawiki 数据库恢复