开发者

pdo page wise fetching

开发者 https://www.devze.com 2023-02-02 02:05 出处:网络
I am using this php pdo wrapper.This is my database class . class Db { private static $_pdoObject = null;

I am using this php pdo wrapper.This is my database class .

class Db
{
    private static $_pdoObject = null;
    protected static $_fetchMode = PDO::FETCH_ASSOC;
    protected static $_connectionStr = null;
    protected static $_driverOptions = array();

    private static $_username = null;
    private static $_password = null;

  public static function setConnectionInfo($schema, $username = null, $password = null, $database = 'mysql', $hostname = 'localhost')
    {
        if($database == 'mysql') {
            self::$_connectionStr = "mysql:dbname=$schema;host=$hostname";
            self::$_username      = $username;
            self::$_password      = $password;
        } else if($database == 'sqlite'){
            // For sqlite, $schema is the file path
            self::$_connectionStr = "sqlite:$schema";
        }

        // Making the connection blank
        // Will connect with provided info on next query execution
        self::$_pdoObject = null;
    }
    public static function getResult($sql, $params = array())
    {
        $statement = self::_query($sql, $params);
        return $statement->fetchAll(self::$_fetchMode);

    }

    private static function _query($sql, $params = array())
    {
        if(self::$_pdoObject == null) {
            self::_connect();
        }

        $statement = self::$_pdoObject->prepare($sql, self::$_driverOptions);
    $arrayjson1=array(
            'success' => false,
           开发者_如何学JAVA 'message'=>'database error '
        );
     $msg= formjson(array(),array(),$arrayjson1);
        if (! $statement) {
            $errorInfo = self::$_pdoObject->errorInfo();
        //~ print_r($errorInfo);
            //~ echo $msg;exit; 
            throw new PDOException("Database error [{$errorInfo[0]}]: {$errorInfo[2]}, driver error code is $errorInfo[1]");
        }

        $paramsConverted = (is_array($params) ? ($params) : (array ($params )));

        if ((! $statement->execute($paramsConverted)) || ($statement->errorCode() != '00000')) {
            $errorInfo = $statement->errorInfo();
                //~ print_r($errorInfo);
            throw new PDOException("Database error [{$errorInfo[0]}]: {$errorInfo[2]}, driver error code is $errorInfo[1]");
        //~ echo $msg;exit;
        }

        return $statement;
    }
}

I am calling this query for getting all the users

     $sql="select  userid,concat_ws(' ',firstname,lastname) as name $fields 
        from users where 1=1   $condition order by updatedon $limit";
    $row=Db::getResult($sql,$query);

I want that by

Input

passing

  1. No. of record per page 2.Page No.

So that it

output

come should be the records of that page only by record per page .

How can i achieve this in pdo.

Please help.Thanks


read the following article. That will help you understand the criteria you are interested in.

paging

0

精彩评论

暂无评论...
验证码 换一张
取 消