using arrays in sql statements

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
nishmgopal
Forum Contributor
Posts: 101
Joined: Tue Mar 03, 2009 9:38 am

using arrays in sql statements

Post by nishmgopal »

HI guys I wanted to know if it is possible to use values of an array in an sql statement.

1st sql:

Code: Select all

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

Code: Select all

$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!
mattpointblank
Forum Contributor
Posts: 304
Joined: Tue Dec 23, 2008 6:29 am

Re: using arrays in sql statements

Post by mattpointblank »

Turn the array into a string and use it like this:

Code: Select all

 
$string = implode(',',$array);
$sql=mysql_query("select * from `table_name` where `a_column`in ($string) ");
 
(from http://forums.mysql.com/read.php?20,132 ... msg-132112)
Post Reply