Is it possible to combine two sql queries?

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

Is it possible to combine two sql queries?

Post 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?
User avatar
aditya2071990
Forum Contributor
Posts: 106
Joined: Thu May 22, 2008 11:30 am
Location: Hyderabad, India
Contact:

Re: Is it possible to combine two sql queries?

Post 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
nishmgopal
Forum Contributor
Posts: 101
Joined: Tue Mar 03, 2009 9:38 am

Re: Is it possible to combine two sql queries?

Post 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?
User avatar
aditya2071990
Forum Contributor
Posts: 106
Joined: Thu May 22, 2008 11:30 am
Location: Hyderabad, India
Contact:

Re: Is it possible to combine two sql queries?

Post by aditya2071990 »

What exactly are you trying to do with those queries?
User avatar
Inkyskin
Forum Contributor
Posts: 282
Joined: Mon Nov 19, 2007 10:15 am
Location: UK

Re: Is it possible to combine two sql queries?

Post 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.
Post Reply