php - 无法将语句插入数据库

标签 php mysql

我有一些句子。我必须选择包含超过 6 个单词的句子。然后它们将被插入到数据库中。

<?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')");
       }
    }
?>

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

最佳答案

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

$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(),则可以更改它们的分隔符。

生成的查询是这样的:

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.')

关于php - 无法将语句插入数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11250744/

相关文章:

php - 如何使用法语口音对数组进行 json_encode?

java - 在 Java 和 PHP 之间加密/解密字符串

MySql 按其他列上的不同初始值聚合

mysql - SQL 插入和选择语法

mysql - 如何在 Windows 10 上的 xampp 中运行 mysql 查询 - 我没有 mysql 密码

php - Laravel Eloquent 将 2 个表连接在一起

php - 仅在 session 不存在时才加载 session_start()?

php - 如何使用项目列表定义 SOAP 响应的 WSDL 复杂类型

php - 如何将从下拉框中选择的值与数组元素进行比较?

php - 如何使用 MySQLi 获取单列值?