无法将句子插入数据库 [英] Cannot insert sentence to database

查看:78
本文介绍了无法将句子插入数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些句子.我必须选择包含6个以上单词的句子.然后将它们插入数据库.

I have some sentences. I have to choose the sentences that consist of more than 6 words. and then they will be inserted to database.

<?php
    require_once 'conf/conf.php';
    $text = " Poetry. Do you read poetry while flying? Many people find it relaxing to read on long flights. Poetry can be divided into several genres, or categories. ";
    $sentences = explode(".", $text);
    foreach ($sentences as $sentence) {
       if (count(preg_split('/\s+/', $sentence)) > 6) {
           $save = $sentence. ".";
           $sql = mysql_query("INSERT INTO tb_name VALUES('','$save')");
       }
    }
?>

结果只是数据库中插入的第二句话=>'您在飞行时阅读诗歌吗?许多人发现长途飞行放松阅读.而第三句也应该插入.请帮助我,谢谢:)

The result is only the second sentence that inserted in database => 'Do you read poetry while flying? Many people find it relaxing to read on long flights'. whereas the third sentence also should be inserted. please help me, thank you : )

推荐答案

这是您正在寻找的解决方案.您不能添加多行,因为您的ID值未指定,这是表的关键.由于要将句子添加到同一行,因此需要执行一个查询.

Here is the solution you're looking for. You cannot add multiple rows since your ID value is left unspecified and it is the key into the table. Since you want to add the sentences to the same row, you need to execute one query.

$text = " Poetry. Do you read poetry while flying? Many people find it relaxing to read on long flights. Poetry can be divided into several genres, or categories. ";
$sentences = explode(".", $text); $save = array();
foreach ($sentences as $sentence) {
   if (count(preg_split('/\s+/', $sentence)) > 6) {
       $save[] = $sentence. ".";
   }
}
if( count( $save) > 0) {
    $sql = mysql_query("INSERT INTO tb_name VALUES('','" . implode( ' ', $save) . "')");
}

现在,两个句子都将插入到数据库的同一行中,并以空格分隔.如果将第一个参数修改为implode(),则可以更改它们的分隔符.

Now, both sentences will be inserted into the same row in the database, separated by a space. You can change what they're separated by if you modify the first parameter to implode().

生成的查询是这样的:

INSERT INTO tb_name VALUES('',' Do you read poetry while flying? Many people find it relaxing to read on long flights. Poetry can be divided into several genres, or categories.')

这篇关于无法将句子插入数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆