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 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿