php 如果没有为变量设置值,则将变量设置为任意

2cmtqfgy  于 2022-12-17  发布在  PHP
关注(0)|答案(3)|浏览(119)

此函数用于搜索值refone并分页:

public function get_all($refone,$per_page,$page_offset) {
        $query = "SELECT * FROM ". self::$table_name . " WHERE rq_detail LIKE :refone";
        $query .= " ORDER BY rq_created DESC";
        $query .= " LIMIT $per_page";
        $query .= " OFFSET $page_offset";
        $sth = $this->conn->prepare($query);
        if(empty($refone)){
            $refone = '';
        } else {
            $refone = "%".$refone."%";
        }
        
        $sth->bindParam('refone', $refone);
        $sth->execute();
        return $row = $sth -> fetchAll();
    }

现在的想法是在refone上,如果没有值提供给上述变量,则结果将显示以下错误:

Notice: Undefined index: refone in C:\wamp\www\dms\view\req\index.php on line 25

在这里,如果你看到的情况下,非空的函数,它会显示像值从开始和结束,但在空的情况下,我已经尝试过,但现在为我工作。
注:在空白的情况下,其结果应与以往相同。

m1m5dgzv

m1m5dgzv1#

您在此行中有错误:

$sth->bindParam('refone', $refone);

应该是

$sth->bindParam(':refone', $refone);

带有:
另外,我会将WHERE子句放在条件语句中:

public function get_all($refone,$per_page,$page_offset) {

    if(!empty($refone)){
        $refone_condition = ' WHERE rq_detail LIKE :refone ';
        $refone = "%".$refone."%";
    } else {
        $refone_condition = ' ';
    }

    $query = "SELECT * FROM ". self::$table_name . $refone_condition;
    $query .= " ORDER BY rq_created DESC";
    $query .= " LIMIT " . intval($per_page);
    $query .= " OFFSET " . intval($page_offset);
    $sth = $this->conn->prepare($query);

    if(!empty($refone)){
      $sth->bindParam(':refone', $refone);
    }

    $sth->execute();
    return $row = $sth -> fetchAll();
}

注意,我还使用intval()清理了$per_page和$page_offset,您也可以像使用:refone那样绑定它们,这样prepared语句就会负责清理它们。
更通用的代码也是如此:

public function get_all($refone = NULL, $per_page = NULL, $page_offset = NULL) {

    $args = array();
    $conditions = array();
    $offset_limit_clause = '';

    if(!is_null($refone)){
        $conditions[] = ' rq_detail LIKE ? ';
        $args[] = "%".$refone."%";
    }

    if(!is_null($page_offset)){
        $offset_limit_clause = ' OFFSET ? ';
        $args[] = intval(($page_offset-1) * $per_page); // offset is expressed in pages
                                                      // must convert to records
        if(empty($per_page)) $per_page = 30; // $per_page cannot be 0 if $page_offset > 1
    }

    if(!is_null($per_page)){
        $offset_limit_clause .= ' LIMIT ? ';
        $args[] = $per_page;
    }

    $query = "SELECT * FROM " . self::$table_name . 
    (count($conditions)?" WHERE ":"") . implode(" AND ", $conditions) .
    " ORDER BY rq_created DESC " . $offset_limit_clause;

    $sth = $this->conn->prepare($query);

    $sth->execute($args);
    return $row = $sth -> fetchAll();
}

最后但并非最不重要的一点是,您可以为函数中的参数设置默认值:

public function get_all($refone = NULL, $per_page = 30, $page_offset = 0) {

如果您不传递任何内容,则将使用默认值,但如果您传递NULL或0或“"等空/错误值,则将接收这些值,因此我认为这不是您在这里要查找的内容。

yqyhoc1h

yqyhoc1h2#

试试这个...

public function get_all($refone,$per_page,$page_offset) {
    $query = "SELECT * FROM ". self::$table_name . " WHERE rq_detail LIKE :refone";
    $query .= " ORDER BY rq_created DESC";
    $query .= " LIMIT $per_page";
    $query .= " OFFSET $page_offset";
    $sth = $this->conn->prepare($query);
    if(empty($refone)){
        $refone = '';
    } else {
        $refone = "%".$refone."%";
    }

    $sth->bindParam(':refone', $refone);
    $sth->execute();
    return $row = $sth -> fetchAll();
}
83qze16e

83qze16e3#

您忘记了这个。更正/添加这个...

$refone = '$';

相关问题