简体   繁体   English

php function 限制 mysql 查询结果

[英]php function to limit mysql query results

Hi I'm currently using the following code (1) to LIMIT the number of lines for the results to be displayed.您好我目前正在使用以下代码 (1) 来限制要显示的结果的行数。 But I want to use a different method but it is not working (2) could someone please help me with the correct syntax?但我想使用不同的方法,但它不起作用(2)有人可以帮我正确的语法吗?

Code (1) Current.代码 (1) 当前。

function find_all_limit10() {
    global $db;
    $sql  = "SELECT * FROM stock ";
    $sql .=" ORDER BY po_date DESC";
    $sql .=" LIMIT 10";
    return find_by_sql($sql);
}

Code (2) Desired but missing LIMIT 10 and ORDER BY代码 (2) 需要但缺少 LIMIT 10 和 ORDER BY

function find_all_limit10($table) {
    global $db;
    if (tableExists($table)) {
        return find_by_sql("SELECT * FROM {$db->escape($table)}"); 
    }
}

Any help will be much appreciated.任何帮助都感激不尽。 Thank you谢谢

I think this is what you are trying to do.我认为这就是你想要做的。 In PHP you can join strings using .在 PHP 中,您可以使用. So we will append the result of the $db->escape($table) string to the end of the SELECT Statement.所以我们将 append 的结果$db->escape($table)字符串到 SELECT 语句的末尾。

function find_all_limit10($table) {
    global $db;
    $sql  = "SELECT * FROM ". $db->escape($table);
    $sql .=" ORDER BY po_date DESC";
    $sql .=" LIMIT 10";
    return find_by_sql($sql);
}

Not sure if this is what you are after...不知道这是否是你所追求的......

function find_all_limit10($table) {
    global $db;
    if (tableExists($table)) {
        $data =  find_by_sql("SELECT * FROM " . $db->escape($table) ); 
        // assuming $data is an array of rows
        // keep just 10
        return array_slice($data, 0, 10);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM