homepage Welcome to WebmasterWorld Guest from 54.226.0.225
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Visit PubCon.com
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
PHP: Joining three tables?
bullseye23




msg:4458026
 2:47 am on May 26, 2012 (gmt 0)

I am fairly new to PHP. I am wanting to join three tables. I understand how the query is supposed to be set for the database as follows:

SELECT c.category,s1.subcategory,s2.subcategory2,s3.subcategory3 FROM subcategory3 as s3
RIGHT JOIN subcategory2 as s2 ON s3.j_id=s2.j_id
RIGHT JOIN subcategory as s1 ON s2.s_id=s1.s_id
RIGHT JOIN category as c ON c. c_id=s1.c_id
ORDER BY category,subcategory,subcategory2,subcategory3 ASC

how would i be able to do this through a php function: mysql_query?

 

CoursesWeb




msg:4458297
 5:46 am on May 27, 2012 (gmt 0)

Hi,
From what i understand, something like this:


$conn = mysql_connect('host','user','password');
mysql_select_db('database_name',$conn);
$sql= "SELECT c.category,s1.subcategory,s2.subcategory2,s3.subcategory3 FROM subcategory3 as s3
RIGHT JOIN subcategory2 as s2 ON s3.j_id=s2.j_id
RIGHT JOIN subcategory as s1 ON s2.s_id=s1.s_id
RIGHT JOIN category as c ON c. c_id=s1.c_id
ORDER BY category,subcategory,subcategory2,subcategory3 ASC";
$result= mysql_query($sql, $conn);
if (mysql_errno()) {
echo "<br />". mysql_errno(). " : ". mysql_error(). "<br />";
}
if (mysql_num_rows($result) == 0) {
echo '0 results';
}
else {
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
print_r($row); // to see data with key=>value in each row
echo '<br/>';
}
}

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved