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 ";

????

$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'

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.

in my scenario useid id varchar

block blue, how to write t using joins ? i tried but couldn't figure out

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

wooowwww thanks alotm both of ur solutions worked....thanks man

u owe me :)

I owe you?
Please mark as solved.

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
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.