PHP PDO-如果另一列中存在另一个值,则检查一列中是否存在一个值


PHP PDO - Check whether a value exists within a column if another value is present in another column

我有以下PHP代码

$queryTableProfileUpdate = $dbh->prepare( "SELECT business_profile_id, business_owner_id FROM TABLE_PROFILES WHERE business_profile_id = $businessOwnerID AND business_profile_id = ?");
$queryTableProfileUpdate->bindValue( 1, $businessProfileID);
$queryTableProfileUpdate->execute();
$resultTableProfileUpdate = $queryTableProfileUpdate->fetch();
if(($queryTableProfileUpdate->rowCount()) == 1) {
    echo 'Busines Profile Exists';
}
else {
    echo 'Business profile does NOT exist';
}

这是我的SQL表

---------------------------------------------------------------------------
| ID        | business_profile_id        | business_owner_id              |
---------------------------------------------------------------------------
| 01        | 1234567                    | abcdefghi                      |
---------------------------------------------------------------------------
| 02        | 2345678                    | abcdefghi                      |
---------------------------------------------------------------------------
| 03        | 3456789                    | abcdefghi                      |
---------------------------------------------------------------------------
| 04        | 1234589                    | xyzuvwxyz                      |
---------------------------------------------------------------------------

在我的上表中,business_profile_id是唯一的,而business_owner_id而不是唯一的。

在我的条件语句中,如果business_owner_id存在,我想检查business_profile_id是否存在。

如果说我有business_owner_idabcdefghibusiness_profile_id2345678,我希望条件语句告诉我该记录存在于abcdefghi中。

否则,如果说我有business_owner_idxyzuvwxyzbusiness_profile_id2345678,我希望条件语句告诉我该记录不存在于abcdefghi中。

我没有得到任何错误日志,只是条件语句保持不变。

您在查询中犯了一个错误:

    SELECT business_profile_id, business_owner_id FROM TABLE_PROFILES 
WHERE business_profile_id = $businessOwnerID AND business_profile_id = ?

应该是:

    SELECT business_profile_id, business_owner_id FROM TABLE_PROFILES 
WHERE business_owner_id = $businessOwnerID AND business_profile_id = ?

这是我尝试过的,似乎奏效了:

########## GET PROFILE DETAILS FROM MYSQL ##########
$queryBusinessID = $dbh->prepare("SELECT business_profile_id FROM TABLE_PROFILES WHERE business_owner_id = $businessOwnerIDSession");
$queryBusinessID->execute();
while ($resultBusinessID = $queryBusinessID->fetch()) {
    $businessProfileID = $resultBusinessID['business_profile_id'];
    ########## FORM DECISION ##########
    if($businessProfileID == $businessProfileIDUrl) {
        echo $businessProfileIDUrl .' - Profile exist <br/>'; 
    } else {
        echo $businessProfileIDUrl .' - Profile does not exist <br/>'; 
    }
}

我知道还有其他的最佳解决方案。我对新想法持开放态度。