gpt4 book ai didi

php - 只希望那些事件的结果显示在查询中。 PHP MySQL

转载 作者:行者123 更新时间:2023-11-30 22:18:09 25 4
gpt4 key购买 nike

我是一名新的 MySQL 和 PHP 编码员...大约 7 年前曾经使用过它,但现在它又落入了我的怀抱。目前,代码会拉动学生进行出勤跟踪,其工作方式如下:

$count = sqlValue("SELECT count(*) FROM `attendance` LEFT JOIN `enrollments` ON attendance.`student_class` = enrollments.id Where enrollments.class='".$class."' AND attendance.date='".$date."'");
if ($count == 0)
{
$query = "SELECT CONCAT_WS('', `students`.`fname`, ' ', `students`.`lname`) as `student`, `classes`.`class_name` as class, enrollments.id as eid FROM `students` LEFT JOIN `enrollments` ON students.id = enrollments.student LEFT JOIN `classes` ON classes.id = enrollments.class";
$query.= " WHERE enrollments.class='".$class."'";
$query.= " ORDER BY students.id";

$res = sql($query, $eo);
$tbody = '';$ids=array();
if(db_num_rows($res) > 0)

这很好用,但现在他们只想显示那些“活跃”的。我在数据库中添加了一列并对前端进行编码以将学生标记为活跃。所有这一切都有效,现在我想运行一个只显示活跃学生的查询,然后让我使用一个复选框将他们标记为出现在类(class)中。我尝试了以下方法,但没有用:

$count = sqlValue("SELECT count(*) FROM `attendance` LEFT JOIN `enrollments` ON attendance.`student_class` = enrollments.id Where enrollments.class='".$class."' AND attendance.date='".$date."'");
if ($count == 0)
{
$query = "SELECT CONCAT_WS('', `students`.`fname`, ' ', `students`.`lname`) as `student`, `classes`.`class_name` as class, enrollments.id as eid FROM `students` LEFT JOIN `enrollments` ON students.id = enrollments.student LEFT JOIN `classes` ON classes.id = enrollments.class";
$query.= " WHERE enrollments.class='".$class."'" and enrollments.status <> 'Active';
$query.= " ORDER BY students.id";

$res = sql($query, $eo);
$tbody = '';$ids=array();
if(db_num_rows($res) > 0)

它在第 45 行抛出错误,即 $query.= " WHERE enrollments.class='".$class."'" and enrollments.status <> 'Active';行。

结构是

    Table structure for table `enrollments`
--

CREATE TABLE IF NOT EXISTS `enrollments` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`student` int(10) unsigned DEFAULT NULL,
`class` int(10) unsigned DEFAULT NULL,
`enrollment_date` date NOT NULL,
`status` varchar(40) NOT NULL,
PRIMARY KEY (`id`),
KEY `student` (`student`),
KEY `class` (`class`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=109 ;

/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;

最佳答案

"'" 中有一个额外的引号。

该行应为:

     $query.= " WHERE enrollments.class='".$class."' and enrollments.status <> 'Active'";

关于php - 只希望那些事件的结果显示在查询中。 PHP MySQL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37529883/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com