PHP PDO 准备查询问题

标签 php mysql arrays pdo

只要选择这两个字段,下面的操作就有效。如果两者都没有选择,它就可以工作,但是我的问题是,当仅选择其中一个字段时,它就不起作用。它引发了未绑定(bind)参数的问题。

我尝试将两个变量设置为 0 的错误值,但这不起作用,因为这样查询将从 where = 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;

最佳答案

您可以尝试检查变量是否已设置以及它们是否保存不为 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 ";
}

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

$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();

关于PHP PDO 准备查询问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32537121/

相关文章:

php - Composer 告诉错误的php版本

php - 导出最便宜的价格和相应的数量?

PHP/MySQL 帖子类别

python - 用零填充 1D NumPy 数组以形成 2D 数组

c++ - C++中的多维数组和指针

php - 有没有办法在不知道名字的情况下找出 POST 变量?

php - 从 Sonata-admin 的侧边菜单中删除子管理员

mysql - 更新多个不同的列

mysql - 多次计数错误在哪里?

Python。我无法将数字从 txt 文件转换为带有 int 的验证器