duangu8264 2014-09-16 14:06
浏览 6
已采纳

返回PDO限制字符?

Is it possible in PDO to limit the character return? I'm trying the following but it simply doesn't return a results set no matter which field I put in the LEFT, yet the rest of the query in this case the id and title is returned as expected. Am I missing something?

    $sql = "SELECT LEFT(content,10), id, title FROM posts";
    $query = $this->db->prepare($sql);
    $query->execute();

So I'm loading my model (above) via the controller like so:

    $posts_model = $this->loadModel('PostsModel');
    $posts = $posts_model->getAllPosts();

Then in my view Im echoing out the results like so:

<?php foreach ($posts as $post) { ?>
<tr>
    <td><h3><?php if (isset($post->title)) echo $post->title; ?></h3></td>
    <td><?php if (isset($post->content)) echo $post->content; ?></td>
</tr>

Not sure if this helps any? Like I say I can switch title and content around and it will echo them out it just wont echo the column in the LEFT part of the SQL statement. I've tried the raw mysql query in PHPMYAdmin and it returns the data as I expect just wont work in the live application using PDO.

  • 写回答

1条回答 默认 最新

  • dongshen9058 2014-09-16 14:45
    关注

    If you SELECT LEFT(content,10), then the column name in the result set will be "LEFT(content,10)", so you'd need to access the data as $post->{'LEFT(content,10)'}. To avoid that, give it an alias:

    SELECT LEFT(content,10) AS content ...
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?