nishmgopal Posted March 14, 2009 Share Posted March 14, 2009 HI guys I wanted to know if it is possible to use values of an array in an sql statement. 1st sql: $query1 = "SELECT * FROM Job_ID WHERE Job_Name ='$_SESSION[Job_Name]'"; $result2 = mysql_query($query1) or die ("Couldn't execute query."); while ($row2=mysql_fetch_array($result2)){ $Job_ID1=$row2['Job_ID']; } 2nd Sql: Using the $Job_ID1 variable: $sql2="SELECT * FROM ID_Table WHERE Job_ID IN (SELECT Job_ID FROM Job_ID WHERE Job_ID=$Job_ID1)"; $result3 = mysql_query($sql2) or die ("Couldn't execute query."); while ($row3=mysql_fetch_array($result3)){ $Skill_ID[]=$row3['Skill_ID']; $Weight[]=$row3['Weight']; } Any particular job id has x amount of skill_ids associated with them. What I want to do is use the $skill_id[] variable in an sql statement to fetch all the names associated with all the skill_ids that a particular Job_id has. For example, my job table, the job id 1 has skills 1, 2 and 3. In the skills table the skill_id's (1,2 and 3) are associated with C, Java and C#. And I want to display these. I am baffled! Link to comment https://forums.phpfreaks.com/topic/149388-using-arrays-in-sql-statements/ Share on other sites More sharing options...
btherl Posted March 14, 2009 Share Posted March 14, 2009 Here's one way to do it $sep = '('; $sid_list = ''; foreach ($Skill_ID as $sid) { $sid_list .= "$sep$sid"; $sep = ','; } $sid_list .= ')'; echo $sid_list; Unless I made a mistake in there, you will get a list of ids that you can use in your sql statement. Link to comment https://forums.phpfreaks.com/topic/149388-using-arrays-in-sql-statements/#findComment-784595 Share on other sites More sharing options...
nishmgopal Posted March 14, 2009 Author Share Posted March 14, 2009 thank you for that, I managed to list the id's. But I still dont understand how to use it in the sql statement to get the names for these ids' would it be: SELECT * FROM Skill_ID WHERE Skill_ID=$sid? Thanks in advance. Link to comment https://forums.phpfreaks.com/topic/149388-using-arrays-in-sql-statements/#findComment-784602 Share on other sites More sharing options...
btherl Posted March 14, 2009 Share Posted March 14, 2009 You can use it like this: SELECT * FROM Skill_ID WHERE Skill_ID IN $sid_list" And the statement should look like this: SELECT * FROM Skill_ID WHERE SKill_ID IN (1,2,3) Link to comment https://forums.phpfreaks.com/topic/149388-using-arrays-in-sql-statements/#findComment-784876 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.