使用mysql pdo进行简短预览

时间:2014-02-10 22:32:03

标签: php mysql pdo

我无法使用LEFT()和substr()只使用带有mysql PDO的RIGHT()进行简短预览,但是我需要显示前100个字符的描述而不是最后的那些...哪里是错误在我的代码中?

代码:

<?php 
    $sql = $conn->prepare("SELECT id_blog, titulo, LEFT(blog,100) AS blog, DATE_FORMAT(f_blog, '%d %M %Y') AS f_blog FROM BLOG ORDER BY id_blog DESC");
    $sql->execute();
    while($row = $sql->fetch(PDO::FETCH_ASSOC)){ echo '
    <div class="span9 pad15" id="'.$row["id_blog"].'">
        <div class="row">
            <div class="span1"> 
                <div class="btn btn-medium btn-rounded btn-blog1">'; echo $row["f_blog"]; echo 
                '</div>
            </div>
            <div class="span8">
                <h1 class="post_link"><a href="blog_post.php?id_blog='.$row["id_blog"].'">'; echo $row["blog_titulo"]; echo '</a></h1>
                <p>'; echo $row["blog"]; echo ' ...</p>
                <div class="read_more"><a href="blog_post.php?id_blog='.$row["id_blog"].'" class="btn btn-primary  btn-custom btn-rounded">Leer m&aacute;s &rarr;</a></div>
                <div class="pad30"></div>
            </div>
        </div>
    </div>'; } ?>
  • LEFT(blog, 100) AS blog没有显示任何内容
  • substr(blog, 0,100) AS blog没有显示任何内容
  • RIGHT(blog,100) AS blog显示最后100个字符

这里LEFT()在同一网页中工作得很好但在第一页中没有用。

CODE:

<?php 
    $sql = "SELECT id_oferta, oferta_titulo, oferta_subtitulo, LEFT(oferta_mensaje, 50) AS oferta_mensaje, oferta_precio, oferta_foto FROM OFERTAS ORDER BY id_oferta DESC";
    $result = $conn->query($sql);
    while($row = $result->fetch(PDO::FETCH_ASSOC)) { ?>
<?php if (!empty($row['id_oferta'])) { echo '
<div class="span3" id"'.$row["id_oferta"].'">
    <div class="tile">
        <div>
            <img src="../admin/assets/img/ofertas/'.$row["oferta_foto"].'" alt="'.$row["oferta_titulo"].'" class="img-circle" style="max-width:100px; max-height:100px;" />
        </div>
        <a href="#myModal" id"'.$row["id_oferta"].'" role="button" class="btn btn-info" data-toggle="modal"><span>'; echo $row["oferta_titulo"]; echo '</span></a>
        <h6><small>'; echo $row["oferta_subtitulo"]; echo '</small></h6>
        <p>'; echo $row["oferta_mensaje"]; echo '</p>
        <h5><div class="intro-icon-disc"><a href="#myModal" id"'.$row["id_oferta"].'" role="button" class="btn btn-primary" data-toggle="modal"><b>$'; echo $row["oferta_precio"]; echo '</b></a></div></h5>
    </div>
    <div class="pad25"></div>
</div>'; } ?>
<div id="myModal" class="modal hide fade" id"<?php echo $row["id_oferta"]; ?>" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-hidden="true"></button>
        <h3 id="myModalLabel"><b>Cup&oacute;n para : <?php echo $row["oferta_titulo"]; ?></b></h3>
    </div>
    <div class="modal-body">
        <form name="cupon" id="cupon" method="post">
            <div class="row-fluid grid">
                <div class="span6">
                    <label><b>Nombres : </b></label><input type="text" class="input-block-level" name="nombres" />
                </div>
                <div class="span2">
                </div>
                <div class="span4">
                    <label><b>DUI : </b></label><input type="text" class="input-block-level" name="dui" />
                </div>
            </div>
            <div class="row-fluid grid">
                <div class="span6">
                    <label><b>Correo : </b></label><input type="text" class="input-block-level" name="corelectronico" />
                </div>
            </div>
            <input type="hidden" name="of_titulo" type="text" value="<?php echo $row['oferta_titulo']; ?>" />
            <input type="hidden" name="of_stitulo" type="text" value="<?php echo $row['oferta_subtitulo']; ?>" />
            <input type="hidden" name="of_mensaje" type="text" value="<?php echo $row['oferta_mensaje']; ?>" />
            <input type="hidden" name="of_precio" type="text" value="<?php echo $row['oferta_precio']; ?>" />
            <input type="hidden" name="of_foto" type="text" value="<?php echo $row['oferta_foto']; ?>" />                   
            <div class="row-fluid grid">
                <div class="span4" id="div1">
                    <input type="hidden" name="action" value="create_cupon" />
                    <button class="positive btn btn-inverse" type="submit" id="enviar" name="enviar">
                        <i class="icon icon-save icon-white"></i> 
                        Grabar Cup&oacute;n
                    </button>
                </div>
                <div class="span4">
                </div>
                <div class="span4" id="div2">
                    <a class="negative btn btn-info" href="impcupon.php" target="popup" onClick="window.open(this.href, this.target, "scrollbars=yes,toolbar=no,menubar=no,status=no,width=parent,height=parent"); return false;" onclick="boton(this)" id="imprimir">
                        <i class=" icon-print icon-white"></i>
                        Imprimir Cup&oacute;n
                    </a>
                </div>
            </div>
        </form>
        <div id="loading_cupon" style="display:none;"><img src="img/loaders/loader.gif" /></div>
    </div>
    <div class="modal-footer">
        <button class="btn" data-dismiss="modal" aria-hidden="true">
            Cerrar
        </button>
    </div>
</div><?php } ?> 

1 个答案:

答案 0 :(得分:1)

正如您的评论中提到的,oferta_mensajelongtext类型。在这种情况下,您可以使用CAST()

"SELECT id_oferta, oferta_titulo, oferta_subtitulo,
        CAST(oferta_mensaje as char(50))
        AS oferta_mensaje, oferta_precio, oferta_foto 
        FROM OFERTAS ORDER BY id_oferta DESC";