我有两张桌子:
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,并且无法将内容数组插入表中。请帮帮我。非常感谢你:))
答案 0 :(得分: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++;
}
}