xxreenaxx1 Posted March 30, 2011 Share Posted March 30, 2011 I am trying to run this query on a webpage but I am getting an error. So I tested this on mysql and this is giving me error as well. #1064 - 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 'IF EXISTS(SELECT * FROM user u, subject s, test t, question q, answer a WHERE u.' at line 1 IF EXISTS (SELECT * FROM user u, subject s, test t, question q, answer a WHERE u.Use_ID = 1 AND t.Sub_ID = 1 AND s.Sub_ID = t.Sub_ID AND q.Que_ID = a.Que_ID) am I doing anything wrong. Quote Link to comment https://forums.phpfreaks.com/topic/232155-if-exists/ Share on other sites More sharing options...
Adam Posted March 30, 2011 Share Posted March 30, 2011 You cannot use an IF statement in MySQL out of a routine (i.e. stored procedure or function). Also EXISTS is for use on sub-queries. What exactly is it's purpose in being there? Quote Link to comment https://forums.phpfreaks.com/topic/232155-if-exists/#findComment-1194303 Share on other sites More sharing options...
JustLikeIcarus Posted March 30, 2011 Share Posted March 30, 2011 Looks like your trying to use MS-SQL on MySQL. Quote Link to comment https://forums.phpfreaks.com/topic/232155-if-exists/#findComment-1194312 Share on other sites More sharing options...
xxreenaxx1 Posted March 30, 2011 Author Share Posted March 30, 2011 oh.. I want to check if the user has already taken a question. This is the reason why I am compairing a.Que_ID = q.Que_ID. So if the person has taken the exam, this will not be displayed. Quote Link to comment https://forums.phpfreaks.com/topic/232155-if-exists/#findComment-1194324 Share on other sites More sharing options...
Adam Posted March 30, 2011 Share Posted March 30, 2011 Well then you should just let the query return 0 rows and check for an empty result set within the application. Quote Link to comment https://forums.phpfreaks.com/topic/232155-if-exists/#findComment-1194325 Share on other sites More sharing options...
xxreenaxx1 Posted March 30, 2011 Author Share Posted March 30, 2011 I dont know how implement that?? :S Quote Link to comment https://forums.phpfreaks.com/topic/232155-if-exists/#findComment-1194335 Share on other sites More sharing options...
Adam Posted March 30, 2011 Share Posted March 30, 2011 Show us the PHP you're using to make the query. Quote Link to comment https://forums.phpfreaks.com/topic/232155-if-exists/#findComment-1194339 Share on other sites More sharing options...
JustLikeIcarus Posted March 30, 2011 Share Posted March 30, 2011 $link = mysql_connect("localhost", "mysql_user", "mysql_password"); mysql_select_db("database", $link); $result = mysql_query("SELECT * FROM user u, subject s, test t, question q, answer a WHERE u.Use_ID = 1 AND t.Sub_ID = 1 AND s.Sub_ID = t.Sub_ID AND q.Que_ID = a.Que_ID", $link); $num_rows = mysql_num_rows($result); if($num_rows) { } else { } Quote Link to comment https://forums.phpfreaks.com/topic/232155-if-exists/#findComment-1194342 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.