转换时我做错了什么 [英] what is it that i am doing wrong while converting

查看:102
本文介绍了转换时我做错了什么的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要将此mysql转换为pdo,我尝试了以下操作,但是我猜错了,因为我没有显示任何结果.是的,对你们中的一个人来说可能很容易,但是pdo对我来说是个新闻,所以感谢您的帮助:)

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 :)

实际代码

$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
            }
            ?>

我尝试了什么

 $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?

推荐答案

prepare()execute()

准备好的语句基本上是这样的:

Prepared statements basically work like this:

  1. 准备:创建一个SQL语句模板并将其发送到 数据库.某些值未指定,称为参数 (标记为?").示例:

  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(?, ?, ?)

数据库解析,编译并执行查询优化 SQL语句模板,并在不执行的情况下存储结果 它

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

执行:稍后,应用程序将这些值绑定到 参数,数据库执行该语句.应用程序 可以用不同的次数执行该语句多次 值

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

尝试以下代码

<?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";
}
?>

这篇关于转换时我做错了什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆