我想在php页面上显示不同的消息,基于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 :(得分: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 :(得分: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 {....
答案 2 :(得分: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";
}
答案 3 :(得分: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";
答案 4 :(得分: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;
答案 5 :(得分: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
}