 |
การ join ตาราง 3 ตาราง ทำไม่ได้อ่าคร่า อาจจะเขียนโค๊ดผิด ลองๆๆเขียนดู ได้แต่ 2ตารางอ่าค่ะ |
|
 |
|
|
 |
 |
|
Code (PHP)
$strSQL = "
SELECT A.Model_Name,B.Car,C.Brand_Name
FROM model A
LEFT JOIN brand B on (A.ID_Brand = B.ID_Brand)
LEFT JOIN car C on (A.ID_Car = C.ID_Car )";
ครับ
|
 |
 |
 |
 |
Date :
2009-09-30 05:44:12 |
By :
danya |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
$strSQL = "
SELECT A.Model_Name,B.Brand_Name,C.Car
FROM model A
LEFT JOIN brand B on (A.ID_Brand = B.ID_Brand)
LEFT JOIN car C on (A.ID_Car = C.ID_Car)";
error อ่ะค่ะ
|
 |
 |
 |
 |
Date :
2009-09-30 06:27:24 |
By :
dektatee |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
error อะไรอะ บอกด้วยซิครับ
|
 |
 |
 |
 |
Date :
2009-09-30 06:34:12 |
By :
danya |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in C:\AppServ\www\KPS\admin\model.php on line 315
อ่ค่ะ
<?
$objConnect = mysql_connect("localhost","root","123456") or die("Error Connect to Database");
$objDB = mysql_select_db("vehicles");
$cs1 = "SET character_set_results=tis620";
mysql_query($cs1) or die('Error query: ' . mysql_error());
$cs2 = "SET character_set_client = tis620";
mysql_query($cs2) or die('Error query: ' . mysql_error());
$cs3 = "SET character_set_connection = tis620";
mysql_query($cs3) or die('Error query: ' . mysql_error());
//$strSQL = "SELECT * FROM model";
$strSQL = "
SELECT A.Model_Name,B.Brand_Name,C.Car
FROM model A
LEFT JOIN brand B on (A.ID_Brand = B.ID_Brand)
LEFT JOIN car C on (A.ID_Car = C.ID_Car)";
//$strSQL="SELECT A.Dep_Name,B.Nm_Fac FROM department A LEFT JOIN faculty B ON(A.ID_Fac=B.ID_Fac)";
//$strSQL = "select A.Model_Name,B.Car,C.Brand_Name from model A left join brand B on (A.ID_Brand = A.ID_Brand) left join car C on (A.ID_Car) = C.ID_Car ";
$objQuery = mysql_query($strSQL) or die ("Error Query [".$strSQL."]");
$Num_Rows = mysql_num_rows($objQuery);
$Per_Page = 8; // Per Page
$Page = $_GET["Page"];
if(!$_GET["Page"])
{
$Page=1;
}
$Prev_Page = $Page-1;
$Next_Page = $Page+1;
$Page_Start = (($Per_Page*$Page)-$Per_Page);
if($Num_Rows<=$Per_Page)
{
$Num_Pages =1;
}
else if(($Num_Rows % $Per_Page)==0)
{
$Num_Pages =($Num_Rows/$Per_Page) ;
}
else
{
$Num_Pages =($Num_Rows/$Per_Page)+1;
$Num_Pages = (int)$Num_Pages;
}
$strSQL .=" order by ID_Brand ASC LIMIT $Page_Start , $Per_Page";
$objQuery = mysql_query($strSQL);
?>
</p>
<p> </p> <style type="text/css">
/* class สำหรับแถวส่วนหัวของตาราง */
/* class สำหรับแถวแรกของรายละเอียด */
.tr_odd{
background-color:#FFFFFF;
}
/* class สำหรับแถวสองของรายละเอียด */
.tr_even{
background-color:#F2F2F2;
}
</style>
<table width="558" border="0" align="center"id="mytable">
<tr>
<th width="227" bgcolor="#0000FF"> <div align="center" class="style36">ประเภทยานพาหนะ</div></th>
<th width="115" bgcolor="#0000FF"> <div align="center" class="style36">ยี่ห้อ</div></th>
<th width="137" bgcolor="#0000FF"> <div align="center" class="style36">ชื่อรุ่น</div></th>
</tr>
<?
while($objResult = mysql_fetch_array($objQuery)) // บรรทัดนี้
{
?>
<tr>
<td><div align="center"><?=$objResult["Car"];?></td>
<td><div align="center"><?=$objResult["Brand_Name"];?></td>
<td><div align="center"><?=$objResult["Model_Name"];?></div></td>
</tr>
<?
}
?>
</table>
<div align="center"><br>
ข้อมูลมี
<?= $Num_Rows;?>
ระเบียน :
<?=$Num_Pages;?>
หน้า :
<?
if($Prev_Page)
{
echo " <a href='$_SERVER[SCRIPT_NAME]?Page=$Prev_Page'><< Back</a> ";
}
for($i=1; $i<=$Num_Pages; $i++){
if($i != $Page)
{
echo "[ <a href='$_SERVER[SCRIPT_NAME]?Page=$i'>$i</a> ]";
}
else
{
echo "<b> $i </b>";
}
}
if($Page!=$Num_Pages)
{
echo " <a href ='$_SERVER[SCRIPT_NAME]?Page=$Next_Page'>Next>></a> ";
}
mysql_close($objConnect);
?>
|
 |
 |
 |
 |
Date :
2009-09-30 07:02:26 |
By :
dektatee |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ต้องใช้
select * from tb1
INNER JOIN tb2 ON tb2.field1=tb1.field1
|
 |
 |
 |
 |
Date :
2009-09-30 08:35:22 |
By :
yod_mk |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
จาก ERROR ที่ขึ้นแสดงว่าไป Query แล้วไม่มีค่าออกมาครับ
ตรงนี้เป็นบรรทัดเดียวกันเปล่าครับ หรือขึ้นบรรทัดใหม่ครับ
[/php]Code (PHP)
<?
$strSQL = "
SELECT A.Model_Name,B.Brand_Name,C.Car
FROM model A
LEFT JOIN brand B on (A.ID_Brand = B.ID_Brand)
LEFT JOIN car C on (A.ID_Car = C.ID_Car)";
//$strSQL="SELECT A.Dep_Name,B.Nm_Fac FROM department A LEFT JOIN faculty B ON(A.ID_Fac=B.ID_Fac)";
//$strSQL = "select A.Model_Name,B.Car,C.Brand_Name from model A left join brand B on (A.ID_Brand = A.ID_Brand) left join car C on (A.ID_Car) = C.ID_Car ";
?>
-------------------------------------------------------------------------------------------------------
ลองเขียนอย่างนี้ดูนะครับ
Code (PHP)
<?
$strSQL = "SELECT A.Model_Name,B.Brand_Name,C.Car";
$strSQL.="FROM model A LEFT JOIN brand B ON(A.ID_Brand=B.ID_Brand)";
$strSQL.="LEFT JOIN car C ON(A.ID_Car=C.ID_Car)";
?>
|
 |
 |
 |
 |
Date :
2009-09-30 08:38:10 |
By :
onizike |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|