【问题标题】:PHP MySQL SELECT text truncatedPHP MySQL SELECT 文本被截断
【发布时间】:2010-12-03 11:19:14
【问题描述】:

我有一个 MySQL 表,其中包含“text”类型的“full_description”列。 在那一栏中是一个标准的 lorem ipsum(加上单词 END):

Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et 多洛尔·麦格纳·阿里夸。 Ut enim ad minim veniam, quis nostrud 练习 乌兰科劳里斯 nisi ut aliquip ex ea commodo 后果。立即拒绝 dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur。例外罪 occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit 动画id est labourum。结束

然而,当在 php 中对其进行选择时,它只检索到这么多:

Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et 多洛尔·麦格纳·阿里夸。 Ut enim ad minim veniam, quis nostrud 练习 乌兰科劳里斯 nisi ut aliquip ex ea commodo 后果。立即拒绝 多乐我

这是检索它的 php 代码:

function getPostingDetails($posting_id){
        $getPosting = $this->PLAST->prepare('SELECT posting_id, poster_id, title, short_description, full_description FROM postings WHERE posting_id=?');
        $getPosting->bind_param('i',$posting_id);
        $getPosting->execute();
        $getPosting->bind_result($row['posting_id'],$row['poster_id'],$row['title'],$row['short_description'],$row['full_description']);
        $getPosting->fetch();
        $getPosting->close();
        return $row;        
    }

这是我得到的数组:

Array ( [posting_id] => 1 [poster_id] => 1 [title] => Test 1 [short_description] => This is a short description. [full_description] => Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor i )

其余字段都很好。 我究竟做错了什么?或者是否有我不知道的限制 SELECT 语句的设置或功能?在 MySQL 中?在 PHP mysqli 中?

谢谢

这是根据要求提供的表结构:

CREATE TABLE `postings` (
  `posting_id` int(11) NOT NULL AUTO_INCREMENT,
  `poster_id` int(11) NOT NULL,
  `title` tinytext NOT NULL,
  `short_description` tinytext NOT NULL,
  `full_description` text NOT NULL,
  PRIMARY KEY (`posting_id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1

【问题讨论】:

  • 你能发布表定义吗? SELECT 返回的文本有 256 个字符。你没有限制吗?另一件事:如何将数据插入数据库?
  • 我刚刚添加了它。我看不到限制在哪里...请注意,在 phpMyAdmin 中,full_description 完全显示。所以它被正确保存。只是在执行 SELECT 时被截断。
  • 你使用的是什么框架?我认为这是对数据类型 TEXT 的一些默认限制。
  • @Lukasz,您可能希望添加 cmets 作为问题的答案,因为您正在努力寻找解决方案。 =)
  • 不确定你所说的框架是什么意思,但我在 Windows 上使用 XAMPP(这是我的本地测试环境)。

标签: php sql mysql types


【解决方案1】:

我在使用 group_concat 并设置 group_concat 的最大长度时遇到了类似的问题,这对我有帮助。刚刚在我的选择查询之前执行了这个查询。

SET GROUP_CONCAT_MAX_LEN=10000;

【讨论】:

  • 这解决了我的问题,实际上我需要增加到 40000。
【解决方案2】:

您使用的框架是什么?我认为这是对数据类型 TEXT 的一些默认限制。

我所说的框架是指访问数据库的 PHP 库:

    $getPosting->bind_param(...);
    $getPosting->execute();
    $getPosting->bind_result(...);
    $getPosting->fetch();
    $getPosting->close();

以上是来自某个库的函数。而且最有可能的是这个库对TEXT字段的长度有默认限制。

已编辑:

我发现 SQL Server 和 PHP here 存在类似问题。他们建议这样做:

SELECT CAST(F AS TEXT) AS F FROM

也许你可以做相反的事情:

SELECT ..., CAST(full_description AS VARCHAR) FROM postings

【讨论】:

  • 这是来自 php 的原生“mysqli”。这些方法来自 MySQLi 语句 (php.net/manual/en/class.mysqli-stmt.php)。
  • 我找到了类似的错误描述。我编辑了我的答案。
  • 我使用的是 MySQL,而不是 Microsoft SQL。在 MySQL 中,'text' 比 'varchar' 大得多,它是 65,535 个字符。
  • 是的,我知道。这正是我所发现的。也许它会以某种方式帮助你。这就是我能提供的全部帮助。
【解决方案3】:

尝试在获取空字符串并将其存储到 php 之前对其进行初始化......我遇到了类似的问题,它只会存储字符串的第一个字母,但是当我在存储之前初始化 var 时成功了

【讨论】:

    【解决方案4】:

    而不是使用数组来存储结果,如果您使用实际变量(即替换此)是否有任何区别:$getPosting->bind_result($row['posting_id'],$row['poster_id'],$row['title'],$row['short_description'],$row['full_description']); 有了这个: $getPosting->bind_result($posting_id,$poster_id,$title,$short_description,$full_description); 看看这有什么不同?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-12-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-10-30
      • 1970-01-01
      相关资源
      最近更新 更多