We're a community of 1077K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,076,154 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

error in sql syntax, php

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
1 Hour
Discussion Span
8 Months Ago
Last Updated
9
Views
Question
Answered
HunainHafeez
Posting Whiz in Training
256 posts since Aug 2012
Reputation Points: -3
Solved Threads: 4
Skill Endorsements: 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'

ckchaudhary
Junior Poster in Training
79 posts since Oct 2011
Reputation Points: 15
Solved Threads: 17
Skill Endorsements: 2

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.

blocblue
Practically a Posting Shark
837 posts since Jan 2008
Reputation Points: 272
Solved Threads: 161
Skill Endorsements: 12

in my scenario useid id varchar

HunainHafeez
Posting Whiz in Training
256 posts since Aug 2012
Reputation Points: -3
Solved Threads: 4
Skill Endorsements: 0

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

HunainHafeez
Posting Whiz in Training
256 posts since Aug 2012
Reputation Points: -3
Solved Threads: 4
Skill Endorsements: 0
SELECT `s`.*
FROM `students` `s`
INNER JOIN `department` `d` ON (`d`.`departmentName` = `s`.`department` AND `d`.`userid` = '{$Userid}')
WHERE `s`.`registrationNo` = '{$RegNo}'
blocblue
Practically a Posting Shark
837 posts since Jan 2008
Reputation Points: 272
Solved Threads: 161
Skill Endorsements: 12

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

u owe me :)

HunainHafeez
Posting Whiz in Training
256 posts since Aug 2012
Reputation Points: -3
Solved Threads: 4
Skill Endorsements: 0

I owe you?
Please mark as solved.

blocblue
Practically a Posting Shark
837 posts since Jan 2008
Reputation Points: 272
Solved Threads: 161
Skill Endorsements: 12

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
jstfsklh211
Junior Poster
100 posts since Apr 2011
Reputation Points: 34
Solved Threads: 27
Skill Endorsements: 1
Question Answered as of 8 Months Ago by blocblue, jstfsklh211 and ckchaudhary

This question has already been solved: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page rendered in 0.0891 seconds using 2.78MB