我遇到了一種情況,我無法弄清楚運行if/else查詢的最佳方式。首先,我不確定我是否可以用sql語句運行if/else。以下是我目前擁有的。通過這個過程,我只在查詢周圍添加了if/else。If/else for INSERT查詢
基本上就是我想要做的是運行第一個查詢,如果它等於$user_id
否則,運行一樣,這是我想要做user_id
的價值 - 等於$profile_viewer
(數據庫列不是上面列出的變量)。
因此,概括地說,如果我能做到這一點,我只是不知道如何修改我的執行:
要包括$profile_viewer
是爲其他查詢參數。
$select_img_stmt->execute(array($user_id));
$user_id = (isset($_SESSION['user']) ? $_SESSION['user'] : "");
$profile_viewer = $_GET['user'];
if ($profile_viewer == $user_id) {
$img_select_sql = "
SELECT *
FROM profile_img
WHERE user_id = ?
ORDER BY id DESC
LIMIT 1
";
}
else {
//echo "This is not your image";
$img_select_sql = "
SELECT *
FROM profile_img
WHERE user_id = ?
ORDER BY id DESC
LIMIT 1
";
}
if ($select_img_stmt = $con->prepare($img_select_sql)) {
$select_img_stmt->execute(array($user_id));
更新的代碼:
$user_id = (isset($_SESSION['user']) ? $_SESSION['user'] : "");
$profile_viewer = $_GET['user'];
if ($profile_viewer == $user_id) {
/*$img_select_sql = "
SELECT *
FROM profile_img
WHERE user_id = ?
ORDER BY id DESC
LIMIT 1
";*/
$img_select_sql = "
SELECT i.*
FROM profile_img i
WHERE user_id IN (?, ?)
ORDER BY id DESC
LIMIT 1;
";
}
else {
//echo "This is not your image";
echo $profile_viewer;
}
if ($select_img_stmt = $con->prepare($img_select_sql)) {
$select_img_stmt->execute(array($user_id, $profile_viewer));
這似乎是好的。會話和GET數組是否包含值?任何錯誤都通過PHP和查詢?當你運行這個時會發生什麼? –
@ Fred-ii-好吧,它在if中起作用,但不起作用。我需要在執行中有兩個不同的參數,'$ user_id'和'$ profile_viewer' – Paul
不知道我的問題出了什麼問題。 – Paul