dsy1971 2011-03-30 03:24
浏览 57

mysql php preg_replace,正则表达式

I have some sql like this:

select o.customer_id, o.reference, o.firstname, o.lastname, os.name as status, o.date_modified, o.total as last_purchase_amount, sum(o.total) as total, count(o.order_id) as number_of_orders, o.currency, o.value from `order` o left join order_status os on o.order_status_id = os.order_status_id where os.language_id = '1' group by customer_id order by date_modified desc

There is a function using the following code to calculate the page index

function splitQuery($sql, $page = '1', $max_rows = '20') {
        $count = $this->getRow(preg_replace(array('/select(.*)from /Asi', '/order by (.*)/i'), array('select count(*) as total from ', ''), $sql, 1));
        $pages = ceil($count['total'] / (int)$max_rows);

it doesn't work with my sql, so i did some testing

$sql = "select o.customer_id, o.reference, o.firstname, o.lastname, os.name as status, o.date_modified, o.total as last_purchase_amount, sum(o.total) as total, count(o.order_id) as number_of_orders, o.currency, o.value from `order` o left join order_status os on o.order_status_id = os.order_status_id where os.language_id = '1' group by customer_id order by date_modified desc
";
echo preg_replace(array('/select(.*)from /Asi', '/order by (.*)/i'), array('select count(*) as total from ', ''), $sql, 1);

it outputs

select count(*) as total from `order` o left join order_status os on o.order_status_id = os.order_status_id where os.language_id = '1' group by customer_id

it seems that "group by customer_id" is where causing the trouble.please help me modify

preg_replace(array('/select(.*)from /Asi', '/order by (.*)/i'), array('select count(*) as total from ', ''), $sql, 1)

or suggest a way to get around it?mysql_num_rows?

The whole function is

function splitQuery($sql, $page = '1', $max_rows = '20') {
    $count = $this->getRow(preg_replace(array('/select(.*)from /Asi', '/order by (.*)/i'), array('select count(*) as total from ', ''), $sql, 1));

    $pages = ceil($count['total'] / (int)$max_rows);

    if (!$page) {
        $page = 1;
    }

    $offset = ((int)$max_rows * ($page - 1));

    $sql .= " limit " . (int)$offset . ", " . (int)$max_rows;

    $this->pages = (int)(($pages > 0) ? $pages : '1');
    $this->total = (int)$count['total']; 
    $this->from  = (int)(($offset > 0 || $count['total'] == 0) ? $offset+1 : '1');

    if ($count['total'] < $max_rows) {
        $this->to = (int)$count['total'];
    }  elseif ($this->pages == $page) {
        $this->to = (int)($offset + $max_rows - ($offset + $max_rows - $count['total']));
    } else {
        $this->to = (int)($offset + $max_rows);
    }

    return $sql;
}

I prefer to not change anything outside this function, whenever possible..

ok...I have decided to use my own dodgy fix

        //$count = $this->getRow(preg_replace(array('/select(.*)from /Asi', '/order by (.*)/i'), array('select count(*) as total from ', ''), $sql, 1));

        $this->result = mysql_query($sql);
        $count = array(
            'total' => mysql_num_rows($this->result)
        );
  • 写回答

1条回答 默认 最新

  • druzuz321103 2011-03-30 04:07
    关注

    Please take a look at my answer here: How to get query to transfer to subsquent pages when paginating results

    on paginating MySQL query results.

    评论

报告相同问题?

悬赏问题

  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教