0

I am working in php along with Mysql in backend, why this query is giving error : You have some problem in mysql syntax, check manual,

this is that query:

$Userid= $_SESSION['userid'];
   echo $Userid; 
   $Dep   = "select departmentName from department where userid='$Userid' ";
   $qry   ="select registrationNo,name,fatherName,cnic,gender,discipline,department,admissionSession,email,password,address,domicile,contactNo,status,currentEmployer,designation,salaryInfo,totalExperience,lastOrganizationname,organizationAdd,organizationPhno,organizationEmail,Remarks from students where registrationNo='$RegNo' AND department= $Dep ";

????

4
Contributors
8
Replies
9
Views
5 Years
Discussion Span
Last Post by jstfsklh211
0

$Dep = "select departmentName from department where userid='$Userid' ";

Most likely the column userid would be an int or bigint. In that case you shouldn't enclose its value in quotes.
So it should rather be .... where userid=$Userid

I can see the same possible error in second sql command as well

where registrationNo='$RegNo' AND department= $Dep

should be where registrationNo=$RegNo AND department= '$Dep'

1

You're using a sub-query for the department. It needs to be wrapped in brackets. E.g.

$query = "select registrationNo,name,fatherName,cnic,gender,discipline,department,admissionSession,email,password,address,domicile,contactNo,status,currentEmployer,designation,salaryInfo,totalExperience,lastOrganizationname,organizationAdd,organizationPhno,organizationEmail,Remarks from students where registrationNo='$RegNo' AND department = ($Dep)";

Alternatively, you could write the query using a join, thus avoiding the need for a sub-query entirely.

1
SELECT `s`.*
FROM `students` `s`
INNER JOIN `department` `d` ON (`d`.`departmentName` = `s`.`department` AND `d`.`userid` = '{$Userid}')
WHERE `s`.`registrationNo` = '{$RegNo}'
1

should be and department in ($dep)

or much simpler

select[fields] from t1 inner join t2 on t1.id = t2.t1id where t1.id = id

Edited by jstfsklh211

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.