我正在尝试检索一个整数表元素(max),取决于课程的3或4,并将其与已输入表格的元素数量进行比较。
以下是我用来获取课程最高入学率和注册课程总数的代码:
//Store the value of the course enrolment limit
$course1 = mysql_real_escape_string($_POST['coursename']);
$qw2 = "SELECT max FROM course WHERE name = '$course1'";
$courselimit = mysql_query($qw2) or die (mysql_error());
//check how many students are enrolled in the course
$qw3 = "SELECT COUNT(*) FROM enrolled WHERE course = '$course1' LIMIT 4";
$totalenrolled = mysql_query($qw3) or die (mysql_error());
不幸的是,即使表条目数超过了课程限制,我的if语句似乎也没有工作,因为它注册为true。
if ($totalenrolled >= $courselimit ){
die('That course has reached the enrolment limit, please select another');
}
EDIT1:这是课程表的转储,我已经尝试了Alexey的解决方案,但遗憾的是该声明似乎尚未运行。我已将问题隔离到$ courselimit变量
--
-- Table structure for table `course`
--
CREATE TABLE `course` (
`name` varchar(50) DEFAULT NULL,
`code` varchar(10) DEFAULT NULL,
`max` int(2) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
--
-- Dumping data for table `course`
--
INSERT INTO `course` (`name`, `code`, `max`) VALUES
('HTML', 'PROG-1288', 4),
('Javascript', 'PROG-2283', 3),
('Dreamweaver', 'MEDA-1380', 4),
('Photoshop', 'PHOT-1382', 3);
这是我将值插入并尝试读取的表的转储:
--
-- Table structure for table `enrolled`
--
CREATE TABLE `enrolled` (
`student` varchar(50) DEFAULT NULL,
`studentnum` varchar(50) DEFAULT NULL,
`course` varchar(50) DEFAULT NULL,
FULLTEXT KEY `student` (`student`,`studentnum`,`course`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
--
-- Dumping data for table `enrolled`
--
INSERT INTO `enrolled` (`student`, `studentnum`, `course`) VALUES
('graham', '987654', 'HTML'),
('bruce', '123456', 'HTML'),
('jane', '111222', 'HTML');
答案 0 :(得分:1)
您需要使用mysql_num_rows()。
//Store the value of the course enrolment limit
$course1 = mysql_real_escape_string($_POST['coursename']);
$qw2 = "SELECT max as `maxitem` FROM course WHERE name = '$course1'";
$res = mysql_query($qw2) or die (mysql_error());
$result = mysql_fetch_assoc($res);
$courselimit = $result['maxitem'];
//check how many students are enrolled in the course
$qw3 = "SELECT COUNT(*) FROM enrolled WHERE course = '$course1'";
$res = mysql_query($qw3) or die (mysql_error());
$totalenrolled = mysql_num_rows($res);
答案 1 :(得分:0)
您是否回复了$totalenrolled
和$courselimit
这两个值,并根据数据表中的数据进行了检查,以确保您获得所需内容?
此外,LIMIT 4
中的$qw3
无法做任何事情 - 因为您使用COUNT(*)
只会有一个结果。
答案 2 :(得分:0)
首先,忘记使用mysql
,而不是mysqli_
或PDO
进行所有操作,更加强大和安全!
You can get basic understanding of mysqli_
from here
现在,问题:
在if
条件下,当您必须在objects
上执行此操作时,您将在value
上进行隔离:
<强>解决方案强>
在比较之前获取值:
$row = mysqli_fetch_array($courselimit,MYSQLI_ASSOC); /* associative array */
$row2 = mysqli_fetch_array($totalenrolled,MYSQLI_ASSOC); /* associative array */
然后比较它们
if ($row['max'] >= $row2['count'] ) //assuming count is column name for 2nd resukt
答案 3 :(得分:0)
您没有获取Mysql对象。试试这个我希望能有效。
//Store the value of the course enrolment limit
$course1 = mysql_real_escape_string($_POST['coursename']);
$qw2 = "SELECT COUNT(*) as count1 FROM course WHERE name = '$course1'";
$courselimit = mysql_fetch_assoc((mysql_query($qw2)) or die (mysql_error());
//check how many students are enrolled in the course
$qw3 = "SELECT COUNT(*) as count2 FROM enrolled WHERE course = '$course1' LIMIT 4";
$totalenrolled = mysql_fetch_assoc(mysql_query($qw3)) or die (mysql_error());
if ($totalenrolled['count2'] >= $courselimit['count1 '] ){
die('That course has reached the enrolment limit, please select another');
}