0

我有 2 张桌子:

Table document
============================================================================
| id_document | document_address |              document_content           |
============================================================================
|      1      |        C         |Example sentences A. Example sentences B.|
|      2      |        D         |Example sentences C. Example sentences D.|
============================================================================

我想基于点分隔 document_content 并将它们插入到带有 doc_id 的句子表中,因此句子表将包含:

Table sentence :
 ======================================================================
 |  id_row  |  id_document |   id_sentence   |       sentence         | 
 ======================================================================
 |     1    |       1      |       0         | Example sentences A    |
 |     2    |       1      |       1         | Example sentences B    |
 |     3    |       2      |       0         | Example sentences C    |
 |     4    |       2      |       1         | Example sentences D    |
 ======================================================================

这是代码:

<?php

require_once 'conf.php';

$doc_id = array();
// get the doc_id
$result = mysql_query("SELECT id_document FROM document");
while($row = mysql_fetch_array($result)) {
   $doc_id[] = $row['id_document'];
}

//get the content
$sql = mysql_query('SELECT document_content FROM document WHERE id_document IN (' . implode(",", $doc_id) . ')') or die(mysql_error());
while ($row = mysql_fetch_array($sql)) {
   $sentence[] = $row['document_content'];
}

$i = 0;
foreach ($sentence as $sentences){
  // Separate based on dot
  $saveSentence = preg_split('/\\.\\s*/', $sentences);
   $q = mysql_query("INSERT INTO sentence VALUES('','$doc_id','$i','$saveSentence','') ");
$i++;
}

?>

但无法读取 doc_id 并且无法将内容数组插入到表中。请帮帮我。非常感谢您 : )

4

1 回答 1

1

你没有获取 doc_id
替换

$sql = mysql_query('SELECT document_content FROM document WHERE id_document IN (' . implode(",", $doc_id) . ')') or die(mysql_error());
while ($row = mysql_fetch_array($sql)) {
   $sentence[] = $row['document_content'];
}

$i = 0;
foreach ($sentence as $sentences){
// Separate based on dot
  $saveSentence = preg_split('/\\.\\s*/', $sentences);
   $q = mysql_query("INSERT INTO sentence VALUES('','$doc_id','$i','$saveSentence','') ");
$i++;
}

$sql = mysql_query('SELECT id_document, document_content FROM document WHERE id_document IN (' . implode(",", $doc_id) . ')') or die(mysql_error());
while ($row = mysql_fetch_array($sql))
{
   $sentence[$row['id_document']] = $row['document_content'];
}

foreach ($sentence as $doc_id => $sentences)
{
  $i = 0;
  foreach(preg_split('/\\.\\s*/', $sentences) as $current_sentence)
  {
    $q = mysql_query("INSERT INTO sentence SET id_document = {$doc_id}, id_sentence = {$i}, sentence = '{$current_sentence}'");
    $i++;
  }
}
于 2012-07-11T14:53:47.597 回答