duanbi3385 2017-08-14 09:35 采纳率: 100%
浏览 13
已采纳

转换[关闭]时我做错了什么

i need to convert this mysql into pdo i tried the following but i guess its wrong as i get no results showing. yes it may be easy for one of you but pdo is news to me so appreciate your help :)

actual code

$rowperpage = 3;

            // counting total number of posts
            $allcount_query = "SELECT count(*) as allcount FROM posts";
            $allcount_result = mysql_query($allcount_query);
            $allcount_fetch = mysql_fetch_array($allcount_result);
            $allcount = $allcount_fetch['allcount'];

            // select first 3 posts
            $query = "select * from posts order by id asc limit 0,$rowperpage ";
            $result = mysql_query($query);

            while($row = mysql_fetch_array($result)){

                $id = $row['id'];
                $title = $row['title'];
                $content = $row['content'];
                $shortcontent = substr($content, 0, 160)."...";
                $link = $row['link'];

            ?>
                <!-- Post -->
                <div class="post" id="post_<?php echo $id; ?>">
                    <h1><?php echo $title; ?></h1>
                    <p>
                        <?php echo $shortcontent; ?>
                    </p>
                    <a href="<?php echo $link; ?>" class="more" target="_blank">More</a>
                </div>

            <?php
            }
            ?>

what i tried

 $rowperpage = 3;

        // counting total number of posts
        //$allcount_query = "SELECT count(*) as allcount FROM posts";
        //$allcount_result = mysql_query($allcount_query);

        $query = "SELECT count(*) FROM posts";
        $stmt = $db->prepare($query);       



        $allcount_fetch = $stmt->fetch(PDO::FETCH_ASSOC);
        $allcount = $stmt->fetchColumn();

        // select first 3 posts
        //$query = "select * from posts order by id asc limit 0,$rowperpage ";
        //$result = mysql_query($query);

        $qry = "select * from posts order by id asc limit 0,$rowperpage ";
        $stm = $db->prepare($qry);  

        while($row = $stm->fetch(PDO::FETCH_ASSOC)){

            $id = $row['id'];
            $title = $row['title'];
            $content = $row['content'];
            $shortcontent = substr($content, 0, 160)."...";
            $link = $row['link'];

can someone show the right way to do it?

  • 写回答

1条回答 默认 最新

  • douganggu4392 2017-08-14 10:27
    关注

    prepare() goes with execute()

    Prepared statements basically work like this:

    1. Prepare: An SQL statement template is created and sent to the database. Certain values are left unspecified, called parameters (labeled "?"). Example:

      INSERT INTO mtTable VALUES(?, ?, ?)

    2. The database parses, compiles, and performs query optimization on the SQL statement template, and stores the result without executing it

    3. Execute: At a later time, the application binds the values to the parameters, and the database executes the statement. The application may execute the statement as many times as it wants with different values

    try with below code

    <?php
    
    $db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
    
    $rowperpage = 3;
    $offset     = 0;
    
    // counting total number of posts
    $query = "SELECT count(id) AS allcount  FROM posts";
    $stmt  = $db->query($query)->fetchColumn();
    
    /******** The ABOVE QUERY LOOKS POINTLESS TO ME AS YOU NOT USING THE RESULTS FROM THAT QUERY*/
    
    // select first 3 posts
    
    $qry = "SELECT * FROM posts ORDER BY id ASC LIMIT ?,? ";
    $stm = $db->prepare($qry);
    $stm->execute(array($offset,$rowperpage));
    $results = $stm->fetchall(PDO::FETCH_ASSOC);
    
    if (count($results) > 0) {
    
        foreach ($results as $row) {
    
            $id           = $row['id'];
            $title        = $row['title'];
            $content      = $row['content'];
            $shortcontent = substr($content, 0, 160) . "...";
            $link         = $row['link'];
    
        }
    } else {
    
        echo "No records found";
    }
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
  • ¥15 wpf界面一直接收PLC给过来的信号,导致UI界面操作起来会卡顿
  • ¥15 init i2c:2 freq:100000[MAIXPY]: find ov2640[MAIXPY]: find ov sensor是main文件哪里有问题吗
  • ¥15 运动想象脑电信号数据集.vhdr
  • ¥15 三因素重复测量数据R语句编写,不存在交互作用
  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了