2011-10-06 43 views
0

存在行我想PHP頁面上顯示不同的信息的基礎上,在MySQL錶行檢查在MYSQL

if (Select * from subscription_details where student id=session id of student) count = 0 

    message: Please sign-up for a subscription package 

    else if 

(select * from subscription_details where ((student id = session_id of student) count > 0) AND ((status=active) count < 1) 

    message: Your subscription has expired, please renew subscription 

需要在這些情況下寫SQL幫助值。

由於提前,

+0

你發佈的SQL有什麼問題?它看起來像會工作。 – ceejayoz

+0

@ceejayoz,它不,只有100%正確的語法工作。這是74.3%正確的:-)。 – Johan

回答

0

,以檢測郵件:

SELECT COUNT(*) AS NumSubscriptions 
FROM subscription_details 
WHERE student_id=123; 

來檢測到期

SELECT COUNT(*) AS NumActive 
FROM subscription_details 
WHERE student id = 123 
AND status=inactive; 
1
$student_id = mysql_real_escape_string($_SESSION['id']); 
$query = "SELECT 
      count(*) as NumberOfSubcriptions 
      count(s2.id) as NumberActive 
      FROM subsciption_details s1 
      LEFT JOIN subsciption_details s2 ON (s1.id = s2.id) 
      WHERE s1.student_id = '$student_id' 
      AND s2.active = 'active' "; 
$result = mysql_query($query); 
$row = mysql_fetch_row($result); 
if ($row['NumberOfSubcriptions'] >= 1) {.....} 
else {} 
if ($row['NumberActive'] >= 1) {do stuff with active subscriptions} 
else {.... 
+0

+1僅用於執行一次查詢。 – NotMe

2

獲得在一個查詢和閱讀兩個變量出:

select 
    case when not exists (
     select * from subscription_details sd1 
     where sd1.student_id = @session_id 
    ) then 1 else 0 end as needsSignup, 

    case when not exists (
     select * from subscription_details sd2 
     where sd2.student_id = @session_id 
     and status = 'active' 
    ) then 1 else 0 end as isExpired 

演示:http://sqlize.com/n0amP9Uxb5

PHP代碼:

// connect to db and run query above 
// read first row into $needsSignup and $isExpired. 

if ($needsSignup) 
{ 
    // signup code 
} 
else if ($isExpired) 
{ 
    // expired code 
} 
1
$pdo = new PDO(...); 

$result = $pdo->query("Select status from subscription_details 
         where student_id=session_id limit 1")->fetch(); 

if (empty($result)) { 
    echo "message: Please sign-up for a subscription package"; 
} else if ($result['status'] != 'active') { 
    echo "message: Your subscription has expired, please renew subscription"; 
} 
1

也許這樣的事情?

$student = mysql_query("Select * from subscription_details where student_id=$session_id;"); 

    $status = mysql_query("select status from subscription_details where student id = session_id AND status=active"); 

    if (count(mysql_fetch_row($student))) 
      print "message: Please sign-up for a subscription package"; 

    else if (count(mysql_fetch_row($status))) 
      print "message: Your subscription has expired, please renew subscription"; 
0

難道你不能按照以下方法做一些事嗎?

$query = mysql_query("SELECT * FROM `subscription_details` WHERE (`student_id` = '$session_id_of_student' AND `status` = 'active')") or die(mysql_error()); 

$amount = mysql_num_rows($query); 

if ($amount == 0) { 

    // Do something 

} else if ($amount > 0) { 

    // Do something else 

}