PHP PDO准备查询问题

时间:2015-09-12 09:11:16

标签: php mysql arrays pdo

只要选择了两个字段,下面的工作就可以了。如果两者都没有被选中,那么我的问题是当只选择一个字段时,它不起作用。它会抛出未绑定的参数问题。

我已经尝试为这两个变量设置一个假值0,但这不会起作用,因为那时查询将从中选择= 0.

想法?

public static function searchProfile($status, $fundamt)
    {
        $database = DatabaseFactory::getFactory()->getConnection();
        $sql = "SELECT profile_id, profile_name, profile_url, finance_fundingtype, finance_equitypercent, finance_loanrate, finance_loanlength, finance_fundingamount, info_tradingstatus, info_elevatorpitch, info_patentable, info_industry, info_industry1, info_industry2, info_industry3, info_industry4, seeker_logo_url FROM profile_seeker WHERE profile_status = '1' ";

        if ($status) {
        $sql .= "AND info_tradingstatus IN (:status) ";
        }
        if ($fundamt) {
            $sql .= "AND finance_fundingamount <= :fundamt ";
        }

        $query = $database->prepare($sql);
        $query->execute(array(':status' => $status, ':fundamt' => $fundamt));
        $profiles = array();
$profiles[$profile->profile_id] = new stdClass();
            $profiles[$profile->profile_id]->profile_id = $profile->profile_id;
            $profiles[$profile->profile_id]->profile_name = $profile->profile_name;
            $profiles[$profile->profile_id]->profile_url = $profile->profile_url;
            $profiles[$profile->profile_id]->finance_fundingtype = $profile->finance_fundingtype;
            $profiles[$profile->profile_id]->finance_equitypercent = $profile->finance_equitypercent;
            $profiles[$profile->profile_id]->finance_loanrate = $profile->finance_loanrate;
            $profiles[$profile->profile_id]->finance_loanlength = $profile->finance_loanlength;
            $profiles[$profile->profile_id]->finance_fundingamount = $profile->finance_fundingamount;
            $profiles[$profile->profile_id]->info_tradingstatus = $profile->info_tradingstatus;
            $profiles[$profile->profile_id]->info_elevatorpitch = $profile->info_elevatorpitch;
            $profiles[$profile->profile_id]->info_patentable = $profile->info_patentable;
            $profiles[$profile->profile_id]->info_industry = $profile->info_industry;
            $profiles[$profile->profile_id]->info_industry1 = $profile->info_industry1;
            $profiles[$profile->profile_id]->info_industry2 = $profile->info_industry2;
            $profiles[$profile->profile_id]->info_industry3 = $profile->info_industry3;
            $profiles[$profile->profile_id]->info_industry4 = $profile->info_industry4;
            $profiles[$profile->profile_id]->seeker_logo_url = $profile->seeker_logo_url;
        }
        return $profiles;

2 个答案:

答案 0 :(得分:0)

您可以尝试检查变量是否已设置,以及它们是否包含非0值且字符串长度大于0的字符串:

if (isset($status) && $status !== 0 && strlen($status) > 0) {
  $sql .= "AND info_tradingstatus IN (:status) ";
}
if (isset($fundamt) && $fundamt!== 0 && strlen($fundamt) > 0) {
  $sql .= "AND finance_fundingamount <= :fundamt ";
}

您也可以尝试手动绑定参数:

$query = $database->prepare($sql);
if (isset($status) && $status !== 0 && strlen($status) > 0) {
  $query ->bindParam(':status',$status);
}
if (isset($fundamt) && $status !== 0 && strlen($fundamt) > 0) {
  $query ->bindParam(':fundamt',$fundamt);
}  
$query->execute();

答案 1 :(得分:0)

您的绑定错误是因为您忘记在绑定上使用相同的if exists语句。

改变这个......

    if ($status) {
    $sql .= "AND info_tradingstatus IN (:status) ";
    }
    if ($fundamt) {
        $sql .= "AND finance_fundingamount <= :fundamt ";
    }

        $profiles[$profile->profile_id]->finance_fundingamount = $profile->finance_fundingamount;
        $profiles[$profile->profile_id]->info_tradingstatus = $profile->info_tradingstatus;

对此...

    if (isset($status) && $status != '') {
    $sql .= "AND info_tradingstatus IN (:status) ";
    }
    if (isste($fundamt) && $fundamt != '') {
        $sql .= "AND finance_fundingamount <= :fundamt ";
    }

        if (isset($status) && $status != '') {
        $profiles[$profile->profile_id]->finance_fundingamount = $profile->finance_fundingamount;
        }
        if (isste($fundamt) && $fundamt != '') {
        $profiles[$profile->profile_id]->info_tradingstatus = $profile->info_tradingstatus;
        }

如果一个或两个文本框都为空,这将停止绑定。

快乐的编码!