Thursday, January 1, 2015

JOINS in PHP Language


INNER JOIN

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>example-php-mysql-bit_count()</title>
</head>
<body>
<?php
include("config.php");
?>


<?php
echo "<h2>List of the book ids, name of the book and category description : </h2>";
echo "<table border='1' style='border-collapse: collapse;border-color: silver;'>";
echo "<tr style='font-weight: bold;'>";
echo "
<td width='200' align='center'>Book ID</td>
<td width='200' align='center'>Name of the book</td>
<td width='200' align='center'>Category description</td>
";
echo "</tr>";


$result = mysql_query("SELECT book_mast.book_id,book_mast.book_name,cate_descrip
FROM book_mast
INNER JOIN category
ON book_mast.cate_id=category.cate_id");


while($row=mysql_fetch_array($result))
{
echo "<tr>";
echo "<td align='center' width='200'>" . $row['book_id'] . "</td>";
echo "<td align='center' width='200'>" . $row['book_name'] . "</td>";
echo "<td align='center' width='200'>" . $row['cate_descrip'] . "</td>";
echo "</tr>";
}
echo "</table>";
?>


</body>
</html>

LEFT JOIN in PHP Language


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">  
<html xmlns="http://www.w3.org/1999/xhtml">  
<head>  
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />  
<title>example-php-mysql-bit_count()</title>  
</head>  
<body>  
<?php
include("config.php");
?>


<?php  
echo "<h2>List of the book ids, name of the book and category description : </h2>";  
echo "<table border='1' style='border-collapse: collapse;border-color: silver;'>";  
echo "<tr style='font-weight: bold;'>";  
echo "
<td width='200' align='center'>Book ID</td>
<td width='200' align='center'>Name of the book</td>
<td width='200' align='center'>Category description</td>
";  
echo "</tr>";  

  
$result = mysql_query("SELECT book_mast.book_id,book_mast.book_name,cate_descrip  
FROM book_mast  
LEFT JOIN category  
ON book_mast.cate_id=category.cate_id");  


while($row=mysql_fetch_array($result))  
{  
echo "<tr>";  
echo "<td align='center' width='200'>" . $row['book_id'] . "</td>";  
echo "<td align='center' width='200'>" . $row['book_name'] . "</td>";  
echo "<td align='center' width='200'>" . $row['cate_descrip'] . "</td>";  
echo "</tr>";  
}  
echo "</table>";  
?>  


</body>  
</html>  


RIGHT JOIN in PHP Language

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">  
<html xmlns="http://www.w3.org/1999/xhtml">  
<head>  
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />  
<title>example-php-mysql-bit_count()</title>  
</head>  
<body>  
<?php
include("config.php");
?>


<?php  
echo "<h2>List of the book ids, name of the book and category description : </h2>";  
echo "<table border='1' style='border-collapse: collapse;border-color: silver;'>";  
echo "<tr style='font-weight: bold;'>";  
echo "
<td width='200' align='center'>Book ID</td>
<td width='200' align='center'>Name of the book</td>
<td width='200' align='center'>Category description</td>
";  
echo "</tr>";  

  
$result = mysql_query("SELECT book_mast.book_id,book_mast.book_name,cate_descrip  
FROM book_mast  
RIGHT JOIN category  
ON book_mast.cate_id=category.cate_id");  


while($row=mysql_fetch_array($result))  
{  
echo "<tr>";  
echo "<td align='center' width='200'>" . $row['book_id'] . "</td>";  
echo "<td align='center' width='200'>" . $row['book_name'] . "</td>";  
echo "<td align='center' width='200'>" . $row['cate_descrip'] . "</td>";  
echo "</tr>";  
}  
echo "</table>";  
?>  


</body>  
</html>  

Database

database name - dbopen
table name - book_mast ( book_id, book_name, cate_id)
table name - category ( cate_id , cate_descrip)

here, book_id , cate_id are Primary ID's or Auto Increment