Welcome to WebmasterWorld Guest from 54.226.25.231

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

PHP: Joining three tables?

     

bullseye23

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

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/>';
}
}
 

Featured Threads

Hot Threads This Week

Hot Threads This Month