2016-11-19 55 views
1

我有一個PHP錯誤,我似乎無法使用此代碼問題與PHP一樣查詢

$_POST['location'] = 'Northern Province'; 
$_POST['transport'] = 'transport'; 

if (isset($_POST['location'])) { 
    $region = "%{$_POST['region']}%"; 
    $transport = "%{$_POST['transport']}%"; 
    $stmt = $conn->prepare("SELECT users.id, users.email, users.profilePhoto, users.userType, organization.userFk, organization.name, organization.specialization from users, organization WHERE users.id = organization.userFk AND organization.specialization LIKE ? AND users.region LIKE ?"); 
    $stmt->bind_param("ss", $transport, $regions); 
    $stmt->execute(); 
    $response["transPortCompanies"] = array(); 
    $stmt->bind_result($id, $email, $profilePhoto, $userType, $userFk, $name, $specialization); 

    while($row = $stmt->fetch()) { 
     $company = array(); 
     $company["id"] = $id; 
     $company["email"] = $email; 
     $company["profilePhoto"] = $profilePhoto; 
     $company["name"] = $name; 
     $company["specialization"] = $specialization; 
     $response["message"] = "Loaded"; 
     $response["error"] = FALSE; 
     array_push($response["transPortCompanies"], $company); 
    } 
    echo json_encode($response); 

顯然,它返回一個空companies列表來解決,但是當我硬編碼像

SELECT users.id, users.email, users.profilePhoto, users.userType, organization.userFk, organization.name, organization.specialization from users, organization WHERE users.id = organization.userFk AND organization.specialization LIKE '%transport%' AND users.region LIKE '%Northern Province%' 

我得到我的輸出,所以想知道我哪裏錯了?我還有一個腳本,做搜索,那就是我是如何實現它

+0

我有我的問題。如果這兩個表之間沒有**關係**,爲什麼不使用加入他們? – Karthi

+0

@Karthi我打算這樣做,但現在我希望查詢能夠正常工作,如果是這樣的話,我打算用加入來替換它 –

+0

使用了錯誤報告http://php.net/manual/en/function。 error-reporting.php可能會避免這個問題。 –

回答

2

我猜你有錯字

bind_param("ss", $transport, $region); // "region", not "regionS" 
+0

@ThandiNgamu,這個解決方案應該適合你。 –

+0

:D PHP不是有一個IDE來檢查這些東西?非常感謝你的工作。 –

+0

@RajdeepPaul謝謝你的努力,我無法做到這一點 –