Page 1 of 1

Is it possible to combine two sql queries?

Posted: Sun Mar 22, 2009 2:32 pm
by nishmgopal
Hi, This is my code:

Code: Select all

 
$sql= "SELECT Job_ID.Job_name, ID_Table.Skill_Name, ID_Table.Weight
FROM Job_ID
JOIN ID_Table USING (Job_ID)
WHERE Job_ID.Job_Name = 'Manager'
";
 
$sql2="SELECT Person_ID.Person_Name, Person_Skill.Skill_Name, Person_Skill.Score
FROM Person_ID
JOIN Person_Skill USING (Person_ID)
WHERE Person_ID.Person_Name='Nish'";
 
 
$result1 = mysql_query($sql,$sql2)
  or die ("Couldn't execute query.");
while ($row1=mysql_fetch_array($result1))
{
    $tblRows .= "<tr>";
    $tblRows .= "<td>{$row['Skill_Name']}</td>";
    $tblRows .= "<td>{$row['Weight']}</td>";
    $tblRows1 .= "<td>{$row1['Score']}</td>";
    $tblRows1 .= "</tr>\n";
  
  }
 
 
I get an invalid arguement error...is what I am trying to do even possible?

Re: Is it possible to combine two sql queries?

Posted: Sun Mar 22, 2009 2:36 pm
by aditya2071990
No, it is not valid. The arguments you can supply to mysql_query are the query string and the link identifier[the mysql connection to which you are referring to]. You'll have to do the "connecting" part inside the query itself...

http://in.php.net/manual/en/function.mysql-query.php

Re: Is it possible to combine two sql queries?

Posted: Sun Mar 22, 2009 2:39 pm
by nishmgopal
so even this wouldnt work?:

Code: Select all

$sql3= $sql . $sql2;
I tried it and got the error: Couldn't execute query.

Could you adivce me on how I can combine the two within the query?

Re: Is it possible to combine two sql queries?

Posted: Sun Mar 22, 2009 3:27 pm
by aditya2071990
What exactly are you trying to do with those queries?

Re: Is it possible to combine two sql queries?

Posted: Sun Mar 22, 2009 3:28 pm
by Inkyskin
Actually this is possible, but with mysqli (which to be honest is the best way to be doing things anyway), have a look here:

http://us2.php.net/manual/en/mysqli.multi-query.php

If your not comfortable with the Object Orientated style of mysqli, you can still use it in a procedural style too.