benji87 Posted May 3, 2007 Share Posted May 3, 2007 Hi guys i can work out why im getting unexpected T_STRING in this query, can someone help? <? $sql =("SELECT * FROM pl_users WHERE username = (SELECT username FROM pl_grp WHERE value = '2')") or die mysql_error()); $result = mysql_query($sql); ?> cheers Link to comment https://forums.phpfreaks.com/topic/49815-solved-sub-query/ Share on other sites More sharing options...
thedarkwinter Posted May 3, 2007 Share Posted May 3, 2007 Hi You have a bracket at the beginning of the $sql variable, and the "or die shoub be on the next line <?php $sql ="SELECT * FROM pl_users WHERE username = (SELECT username FROM pl_grp WHERE value = '2')"; $result = mysql_query($sql) or die(mysql_error()); ?> Cheers, tdw Link to comment https://forums.phpfreaks.com/topic/49815-solved-sub-query/#findComment-244342 Share on other sites More sharing options...
sw0o0sh Posted May 3, 2007 Share Posted May 3, 2007 Um, i dont work much with mysql queries inside mysql queries, but first of all, you are trying to perform a sub search without defining the sub search as a mysql query itsself, plus with all the () you added in addition that could make things harder (with getting the syntax right) here's something i typed out for you <? $result = mysql_query("SELECT * FROM pl_users WHERE username = \"" . mysql_query("SELECT username FROM pl_grp WHERE value = '2'") . "\"); ?> Edit, and yeah the guy above is also correct: <? $result = mysql_query("SELECT * FROM pl_users WHERE username = \"" . mysql_query("SELECT username FROM pl_grp WHERE value = '2'") . "\") or die(mysql_error()); ?> Link to comment https://forums.phpfreaks.com/topic/49815-solved-sub-query/#findComment-244343 Share on other sites More sharing options...
utexas_pjm Posted May 3, 2007 Share Posted May 3, 2007 I'm not sure of your table structure, but if value is not a PK then you'll want your query to look like: SELECT * FROM pl_users WHERE username IN (SELECT username FROM pl_grp WHERE value = '2') Best, Patrick Link to comment https://forums.phpfreaks.com/topic/49815-solved-sub-query/#findComment-244347 Share on other sites More sharing options...
benji87 Posted May 3, 2007 Author Share Posted May 3, 2007 Cheers for the help guys darkwinter and sw0o0sh that query worked great but only returned one result. As when there was more than one value that = 2 it came up with an error saying the result could not be saved because the query returned more than one result. So i guess this is something to do with the PK like Patrick said. So i changed it to the query he gave me but i still only seem to get one result when i should get two. Here is my query as it stands: <?php $sql ="SELECT * FROM pl_users WHERE username IN (SELECT username FROM pl_gpr WHERE value = '2')"; $result = mysql_query($sql) or die(mysql_error()); ?> Cheers Link to comment https://forums.phpfreaks.com/topic/49815-solved-sub-query/#findComment-244352 Share on other sites More sharing options...
benji87 Posted May 3, 2007 Author Share Posted May 3, 2007 My bad ive sorted it thanks for all the help! Link to comment https://forums.phpfreaks.com/topic/49815-solved-sub-query/#findComment-244370 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.