gpt4 book ai didi

php - 即使在 PHP 中多次尝试后,MySQL 连接仍无法正常工作

转载 作者:行者123 更新时间:2023-11-30 23:56:45 25 4
gpt4 key购买 nike

<分区>

我确定我可能犯了小错误,但我无法发现错误。我不明白为什么我不能在 MySQL 中使用连接查询。

我在数据库中有三个关系表:

  1. 主题
  2. class_subject

代码是:

   CREATE  TABLE IF NOT EXISTS `class` (
`ClassID` INT(11) NOT NULL AUTO_INCREMENT ,
`Class` VARCHAR(500) NULL DEFAULT NULL ,
`Section` VARCHAR(500) NULL DEFAULT NULL ,
PRIMARY KEY (`ClassID`))

ENGINE = InnoDB
AUTO_INCREMENT = 15
DEFAULT CHARACTER SET = utf8;


INSERT INTO `class`(`ClassID`, `Class`, `Section`) VALUES ('1','1','A'), ('2','1','B'), ('3','1','C'), ('4','1','D') ;

CREATE TABLE IF NOT EXISTS `Subject` (
`SubjectID` INT(11) NOT NULL AUTO_INCREMENT ,
`Subject` VARCHAR(45) NULL,

PRIMARY KEY (`SubjectID`))


ENGINE = InnoDB
AUTO_INCREMENT = 53
DEFAULT CHARACTER SET = utf8;

INSERT INTO `Subject`(`SubjectID`, `Subject`) VALUES ('1','English'),('2','Hindi'),('3','Maths'),('4','Science'),('5','Social Science'),('6','Sports'),('7','Physics'), ('8','Chemistry'), ('9','Statistics'), ('10','Computers');

和...

    CREATE TABLE IF NOT EXISTS  `class_subject` (
ClassID INT (11) NOT NULL,
SubjectID INT (11) NOT NULL,
PRIMARY KEY (ClassID, SubjectID),
INDEX `subject_order_idx` (`ClassID` ASC) ,
CONSTRAINT `class_subject`
FOREIGN KEY (`ClassID` )
REFERENCES `class` (`ClassID` )ON UPDATE CASCADE,
FOREIGN KEY (`SubjectID` )
REFERENCES `subject` (`SubjectID` )ON UPDATE CASCADE)

ENGINE = InnoDB
AUTO_INCREMENT = 15
DEFAULT CHARACTER SET = utf8;


INSERT INTO `class_subject`(`ClassID`, `SubjectID`) VALUES ('1','1'), ('1','2'), ('1','3'), ('12','4'), ('12','5'), ('12','6');

加入查询:

$ClassID = "1";
$sql = "SELECT subject.SubjectID, subject.Subject FROM subject " .
"LEFT JOIN class_subject" .
"ON subject.SubjectID = class_subject.SubjectID " .
"WHERE class_subject.ClassID = $ClassID " .
"ORDER BY SubjectID ASC";

尽管相同的查询在 phpMyAdmin 中运行良好。

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