เปรียบเทียบค่า ID แล้ว แสดงเป็นชื่อ ช่วยหน่อยครับ SELECT ยังไงดีครับ
คงประมาณนี้ครับ
Code (PHP)
$objConnect = mysql_connect("localhost","root","") or die("Error Connect to Database");
$objDB = mysql_select_db("test");
$strSQL = " SELECT
`tb1`.`FilesID`,
`tb1`.`Name_M`,
`tb1`.`UserID_R`,
`tb2`.`Username`,
`tb2`.`UserID`
FROM
`tb1`
Inner Join `tb2` ON `tb1`.`UserID_R` = `tb2`.`UserID`
";
$objQuery = mysql_query($strSQL) or die ("Error Query [".$strSQL."]");
Date :
2012-08-23 15:16:48
By :
tongkamlekdee
Code (PHP)
$objConnect = mysql_connect("localhost","root","") or die("Error Connect to Database");
$objDB = mysql_select_db("test");
$strSQL = " SELECT table1.*,table2.* FROM table1,table2 WHERE table1. UserID_R = table2.UserID ";
$ojbQuery = mysql_query($strSQL);
while($ojbResult=mysql_fetch_array($ojbQuery))
{
echo$ojbResult["table2.Name_M"];
}
Date :
2012-08-23 16:00:46
By :
sambrazil
Name_M นะผมว่าลบไปเหอะตารางแรก มันซ้ำซ้อนอะ Database
Date :
2012-08-23 16:02:27
By :
sambrazil
จิงๆๆแค่เอาJoin กันมันก้อแสดงแล้วครับ ไม่ต้อง insert
Date :
2012-08-23 16:03:34
By :
sambrazil
Code (PHP)
$SelectTb1=mysql_query("SELECT * FROM tb_1") or die(mysql_error());
while($Tb1=mysql_fetch_array($SelectTb1)){
$SelectTb2=mysql_query("SELECT * FROM tb_2 WHERE UserID='".$Tb1['UserID_R']."'") or die (mysql_error());
$Tb2=mysql_fetch_array($SelectTb2);
echo $Tb2['Username'];
}
แบบนี้รึเปล่า ??
Date :
2012-08-23 16:10:00
By :
Ex-[S]i[L]e[N]t
Code (PHP)
$sql1 = "SELECT * FROM tb_1 ";
$Query1 = mysql_query($sql1);
while($array1 = mysql_fetch_array($Query1))
{
$UserID_R = $array1['UserID_R '] ;
$sql2 = "SELECT * FROM tb_2 where UserID =' ".$UserID_R." ' ";
$Query2 = mysql_query($sql2);
while($array2 = mysql_fetch_array($Query2))
{
echo $array2['Name_M'] ;
}
}
ประวัติการแก้ไข 2012-08-23 20:47:05
Date :
2012-08-23 20:44:11
By :
apisitp
Code (PHP)
<?=$objResult["FilesID"];?>',783,600); return false;" title="">Details</a></center></td>
<td class="tbl5"><center> <? $sql1 = "SELECT * FROM files ";
$Query1 = mysql_query($sql1);
while($array1 = mysql_fetch_array($Query1))
{
$UserID_Receiver = $array1['UserID_Receiver'] ;
$sql2 = "SELECT * FROM member where UserID =' ".$UserID_Receiver." ' ";
$Query2 = mysql_query($sql2);
while($array2 = mysql_fetch_array($Query2))
{
echo $array2['Name'] ;
}
} ?>
ไม่ได้ครับพี่ มันecho ชื่อออกหมดเลยอ่ะ
ผมแนบโคดหน้านี้ด้วยล่ะกัน เพื่อผมทำตรงไหนผิด พี่ๆช่วยแนะผมหน่อยน่ะครับ ติดมา หลายวันล่ะ
Code (PHP)
<?
session_start();
if($_SESSION['UserID'] == "")
{
echo "Please Login!";
exit();
}
?>
<?
mysql_connect("localhost","root","6845");
mysql_select_db("dc");
mysql_query("SET NAMES UTF8");
$strSQL = " SELECT files.*,member.* FROM files,member WHERE files. UserID_Receiver = member.UserID
";
$objQuery = mysql_query($strSQL) or die ("Error Query [".$strSQL."]");
$objResult2 = mysql_fetch_array($objQuery);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel="stylesheet" href="style_uploadfile.css" type="text/css" />
<title>Untitled Document</title>
<script language="javascript">
function js_popup(theURL,width,height) { //v2.0
leftpos = (screen.availWidth - width) / 2;
toppos = (screen.availHeight - height) / 2;
window.open(theURL, "viewdetails","width=" + width + ",height=" + height + ",left=" + leftpos + ",top=" + toppos);
}
</script>
</head>
<body>
<div id="Form">
<div id="header">
</div>
<h1>.::Upload files::.</h1>
<ul id="menu">
<li><a href="index.php?page=UploadFiles"><img src="images/upload.png" /> Upload Files</a></li></td>
<li><a href="ViewFiles.php"><img src="images/list.png" /> View Files</a></li>
<li><a href="ViewFilesonly.php"><img src="images/list.png" /> Only you</a></li>
<li><a href="ViewFilesSent.php"><img src="images/list.png" /> View Files Sent</a></li>
<li><a href="index.php?page=Search"><img src="images/search.png" /> Search</a></li>
<li><a href="index.php?page=Profile"><img src="images/profile.png" /> Profile</a></li>
<li><a href="index.php?page=ChangePassword"><img src="images/change.png" /> Change Password</a></li>
<li><a href="#"><img src="images/manual.gif" /> Manual</a></li>
<li><a href="Logout.php"><img src="images/logout.png" /> Logout</a></li>
</ul>
</div>
<table width="68%" border="0" align="center" cellspacing="1" >
<tr>
<td>
<?
$objConnect = mysql_connect("localhost","root","6845") or die("Error Connect to Database");
$objDB = mysql_select_db("dc");
mysql_query("SET NAMES UTF8");
$strSQL = "SELECT * FROM files ";
$objQuery = mysql_query($strSQL) or die ("Error Query [".$strSQL."]");
$Num_Rows = mysql_num_rows($objQuery);
$Per_Page = 15; // 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 FilesID desc LIMIT $Page_Start , $Per_Page";
$objQuery = mysql_query($strSQL);
?>
<table width="100%" align="center" cellpadding="0" cellspacing="3" class="table">
<tr>
<td colspan="3" style="white-space:normal"><strong><img src="images/formupload.png" align="absmiddle"/> View Files</strong></td>
</tr>
<tr>
<td width="5%" class="tbl5" style="white-space: nowrap;"><div align="center"><strong>No.</strong></div></td>
<td width="10%" class="tbl5" style="white-space: nowrap;"><div align="center"><strong>File</strong></div></td>
<td width="15%" class="tbl5" style="white-space: nowrap;"><div align="center"><strong>Name</strong></div></td>
<td width="10%" class="tbl5" style="white-space: nowrap;"><div align="center"><strong>Day</strong></div></td>
<td width="15%" class="tbl5" style="white-space: nowrap;"><div align="center"><strong>Time</strong></div></td>
<td width="20%" class="tbl5" style="white-space: nowrap;"><div align="center"><strong>By</strong></div></td>
<td width="10%" class="tbl5" style="white-space: nowrap;"><div align="center"><strong>Details</strong></div></td>
<td width="10%" class="tbl5" style="white-space: nowrap;"><div align="center"><strong>ผู้รับ</strong></div></td>
<td width="10%" class="tbl5" style="white-space: nowrap;"><div align="center"><strong>Status</strong></div></td>
</tr>
<?
while($objResult = mysql_fetch_array($objQuery))
{
?>
<tr >
<td class="tbl5"><div align="center"><?=$objResult["FilesID"];?></div></td>
<td class="tbl5"><center>
<?php
$file = $objResult["Filesname"];
$extension = strrchr( $file , '.' );
switch( $extension )
{
case '.jpg':
echo "<img src='images/icon/JPEG-Image-icon.png' />";
break;
case '.JPG':
echo "<img src='images/icon/JPEG-Image-icon.png' />";
break;
case '.pdf':
echo "<img src='images/icon/pdf-icon.png' />";
break;
case '.png':
echo "<img src='images/icon/PNG-Image-icon.png' />";
break;
case '.gif':
echo "<img src='images/icon/GIF-Image-icon.png' />";
break;
case '.doc':
echo "<img src='images/icon/Office-Word-icon.png' />";
break;
case '.docx':
echo "<img src='images/icon/Office-Word-icon.png' />";
break;
case '.xls':
echo "<img src='images/icon/Office-Excel-icon.png' />";
break;
case '.zip':
echo "<img src='images/icon/rar-2.png' />";
break;
case '.rar':
echo "<img src='images/icon/rar-2.png' />";
break;
case '.txt':
echo "<img src='images/icon/TEXT-Image-icon.png' />";
break;
default :
echo 'นามสกุลของไฟล์ไม่อยู่ในรายการตรวจสอบ';
}
?>
</center></td>
<td class="tbl5"><center><?=$objResult["Name_Filesnew"];?></center></td>
<td class="tbl5"><center><?=$objResult["Date"];?></center></td>
<td class="tbl5"><center><?=$objResult["Time"];?></center></td>
<td class="tbl5"><center><a href="viewmember.php?UserID= <?=$objResult["UserID_Sender"];?>" onClick="js_popup('viewmember.php?UserID= <?=$objResult["UserID_Sender"];?>',783,600); return false;" title="Member"><?=$objResult["Name_Sender"];?></a></center></td>
<td class="tbl5"><center><a href="viewdetails.php?FilesID=<?=$objResult["FilesID"];?>" onClick="js_popup('viewdetails.php?FilesID=<?=$objResult["FilesID"];?>',783,600); return false;" title="">Details</a></center></td>
<td class="tbl5"><center> <? $sql1 = "SELECT * FROM files ";
$Query1 = mysql_query($sql1);
while($array1 = mysql_fetch_array($Query1))
{
$UserID_Receiver = $array1['UserID_Receiver'] ;
$sql2 = "SELECT * FROM member where UserID =' ".$UserID_Receiver." ' ";
$Query2 = mysql_query($sql2);
while($array2 = mysql_fetch_array($Query2))
{
echo $array2['Name'] ;
}
} ?> </center></td>
<td class="tbl5"><center><?=$objResult["Status_Files"];?></center></td>
</tr>
<?
}
?>
</table>
<br>
<div class="table" align="center"> Total <?= $Num_Rows;?> Files <?=$Num_Pages;?> Page :</div>
<?
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);
?>
</td>
</tr>
</table>
</body>
</html>
Date :
2012-08-24 09:39:48
By :
cappuczino
codeยาวมาก
Date :
2012-08-24 13:58:38
By :
sambrazil
เปลี่ยน ชื่อเทเบิลมาแล้ว ใครก้ได้ช่วยผมหน่อย
SELECT ยังไงดี ให้เปรียบเทียบทั้ง UserID และ UserID_Receiver ถ้าตรงกันให้ดึง Name.member ออกมา
ประวัติการแก้ไข 2012-08-24 14:48:45
Date :
2012-08-24 14:47:36
By :
cappuczino
Load balance : Server 00