我想弄清楚这段代码中有什么不对,但无法使其正常工作
你能帮我吗?
$email = $_SESSION['email'];
$email = mysql_real_escape_string($email);
$depst = "SELECT dept FROM stud_reg WHERE email='$email'";
$colls = "SELECT coll FROM stud_reg WHERE email='$email'";
$query="SELECT * FROM stud_reg WHERE coll='$coll' AND dept='$depst'";
$evesel="SELECT id FROM events WHERE `group`='($depst)' AND coll_id='($colls)'";
$studsel="SELECT drs_id FROM event_reg WHERE eve_id='$evesel'";
$query="select * from students WHERE nsite_id='$studsel'";
$result=mysql_query($query) or die(mysql_error());
$num=mysql_num_rows($result);
这是我得到的错误
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'k@***.com')' AND coll_id='(SELECT coll FROM stud_reg WHERE email='k@****' at line 1
PS-此代码中的所有表和行都退出
答案 0 :(得分:0)
尝试他的
$query="select * from students WHERE nsite_id='$studsel';";
并且第一个查询从未实际执行,并且您似乎覆盖了第一个$查询。要么“加入”,要么使用子查询,比如
SELECT * FROM t1 WHERE column1 = (SELECT column1 FROM t2);
答案 1 :(得分:0)
"SELECT id FROM events WHERE `group`='".$depst."' AND coll_id='".$colls."'";
答案 2 :(得分:0)
根据您收到的错误消息,我认为这只是单引号和双引号问题 这是更正的查询:
$email = $_SESSION['email'];
$email = mysql_real_escape_string($email);
$depst = 'SELECT dept FROM stud_reg WHERE email="$email"';
$colls = 'SELECT coll FROM stud_reg WHERE email="$email"';
$query="SELECT * FROM stud_reg WHERE coll='$coll' AND dept='$depst'";
$evesel="SELECT id FROM events WHERE `group`='($depst)' AND coll_id='($colls)'";
$studsel="SELECT drs_id FROM event_reg WHERE eve_id='$evesel'";
$query="select * from students WHERE nsite_id='$studsel'";
$result=mysql_query($query) or die(mysql_error());
$num=mysql_num_rows($result);
在查询中放置quotes
时一定要小心。
答案 3 :(得分:0)
您尝试运行的查询是
select * from students WHERE nsite_id='SELECT drs_id FROM event_reg WHERE eve_id='SELECT id FROM events WHERE `group`='(SELECT dept FROM stud_reg WHERE email='$email')' AND coll_id='(SELECT coll FROM stud_reg WHERE email='$email')'''
但即使您确实修复了带引号字符串中引用字符串的问题,这也无法满足您的期望。
我怀疑相反想要将所有这些结合起来,所以也许有点像:
SELECT s.*
FROM students AS s
INNER JOIN event_reg AS er
ON er.drs_id = s.nsite_id
INNER JOIN events AS e
ON er.eve_id = e.id
INNER JOIN stud_reg AS grp
ON grp.dept = e.group
AND grp.coll = e.coll_id
WHERE grp.email='$email'
与SQL的任何语法错误一样,当从PHP运行时,最好先在MySQL中使用查询,然后再尝试将其插入应用程序。
答案 4 :(得分:0)
为什么不尝试单一查询。问题在于您的情况。
$query = "SELECT * FROM students WHERE nsite_id =
( SELECT drs_id FROM event_reg WHERE eve_id = (
SELECT id FROM events WHERE
`group` = (SELECT dept FROM stud_reg WHERE email = '".$email."')
AND
coll_id = (SELECT coll FROM stud_reg WHERE email = '".$email."')
)
)";
$result=mysql_query($query) or die(mysql_error());
$num=mysql_num_rows($result);