  | 
              
	              
	                
  
    
	 
        กรณีที่เราค้นหารายชื่อสมาชิกจากดาต้าเบสแล้ว เราจะexport เป้นไฟล์ CSV ต้องทำยังไงค่ะ     | 
   
  
    |   | 
   
 
 
 
              
  
          
		
     
		
	  
        
             | 
            | 
            | 
             | 
         
        
             | 
                       | 
          
            
               
                 ในบทความก็มีให้ดูเป้นตัวอย่างน่ะครับ การค้นหาก็แค่ส่งตัวแปรไป Where ใน SQL เท่านั้นครับ 
 
Code (PHP) 
<html>
<head>
<title>ThaiCreate.Com PHP & MySQL To CSV</title>
</head>
<body>
<?
$objConnect = mysql_connect("localhost","root","root") or die("Error Connect to Database");
$objDB = mysql_select_db("mydatabase");
$strSQL = "SELECT * FROM customer WHERE Name like '%วีระชัย%' ";
$objQuery = mysql_query($strSQL) or die ("Error Query [".$strSQL."]");
?>
<table width="600" border="1">
  <tr>
    <th width="91"> <div align="center">CustomerID </div></th>
    <th width="98"> <div align="center">Name </div></th>
    <th width="198"> <div align="center">Email </div></th>
    <th width="97"> <div align="center">CountryCode </div></th>
    <th width="59"> <div align="center">Budget </div></th>
    <th width="71"> <div align="center">Used </div></th>
  </tr>
<?
while($objResult = mysql_fetch_array($objQuery))
{
?>
  <tr>
    <td><div align="center"><?=$objResult["CustomerID"];?></div></td>
    <td><?=$objResult["Name"];?></td>
    <td><?=$objResult["Email"];?></td>
    <td><div align="center"><?=$objResult["CountryCode"];?></div></td>
    <td align="right"><?=$objResult["Budget"];?></td>
    <td align="right"><?=$objResult["Used"];?></td>
  </tr>
<?
}
?>
</table>
<?
mysql_close($objConnect);
?>
<div align="center"><br>
  <input name="btnExport" type="button" value="Export" onClick="JavaScript:window.location='phpCSVMySQLExport.php';">
</div>
</body>
</html>
                        
               
               | 
             
            
              
			  			  
			                              
                              
              
                
                     | 
                     | 
                     | 
                 
                
                     | 
                  
                      
                        | Date :
                            2011-05-18 14:16:53 | 
                        By :
                            webmaster | 
                         
                    | 
                     | 
                 
                
                     | 
                     | 
                     | 
                 
                | 
             
           
			         | 
             | 
         
        
             | 
            | 
             | 
             | 
         
          
	    
     
               
		
     
		
	     
	    
     
               
		
     
		
	  
        
             | 
            | 
            | 
             | 
         
        
             | 
                       | 
          
            
               
                 คือว่าค้นหาได้แล้วค่ะ แต่อยากนำไฟล์ที่เราค้นหาได้ ไป export ออกมา 
 
ใช้คำสั่งอารัยค่ะ ในการกำหนด                        
               
               | 
             
            
              
			                              
                              
              
                
                     | 
                     | 
                     | 
                 
                
                     | 
                  
                      
                        | Date :
                            2011-05-18 14:22:22 | 
                        By :
                            NWii | 
                         
                    | 
                     | 
                 
                
                     | 
                     | 
                     | 
                 
                | 
             
           
			         | 
             | 
         
        
             | 
            | 
             | 
             | 
         
          
	    
     
               
		
     
		
	  
        
             | 
            | 
            | 
             | 
         
        
             | 
                       | 
          
            
               
                 ตามตัวอย่างครับ                        
               
               | 
             
            
              
			  			  
			                              
                              
              
                
                     | 
                     | 
                     | 
                 
                
                     | 
                  
                      
                        | Date :
                            2011-05-18 14:40:51 | 
                        By :
                            webmaster | 
                         
                    | 
                     | 
                 
                
                     | 
                     | 
                     | 
                 
                | 
             
           
			         | 
             | 
         
        
             | 
            | 
             | 
             | 
         
          
	    
     
               
		
     
		
	  
        
             | 
            | 
            | 
             | 
         
        
             | 
                       | 
          
            
               
                 Code (PHP) 
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=tis-620" />
</head>
<body>
<form name="frmSearch" method="get" action="<?=$_SERVER['SCRIPT_NAME'];?>">
<table width="572" height="49" border="1">
    <tr>
      <td width="445" height="43">คำค้น :  
        <input name="txtKeyword" type="text" id="txtKeyword" value="<?=$_GET["txtKeyword"];?>"> </td>
      <td width="111"><input type="submit" value="ค้นหา"></td>
    </tr>
  </table>
</form>
<p>
  <?
if($_GET["txtKeyword"] != "")
	{
	
	include("connect.php");
	
	$_SESSION["txtKeyword"] = $_GET["txtKeyword"];
	
	$strSQL = "SELECT * FROM datamem LEFT JOIN dataacc ON (datamem.code = dataacc.code)  WHERE (name LIKE '%".$_GET["txtKeyword"]."%' or uname LIKE '%".$_GET["txtKeyword"]."%')";
	
	
	$objQuery = mysql_query($strSQL);
	
	?>
</p>
<table width="572" border="1" cellpadding="0" cellspacing="0">
<tr align="center">
		<th width="80" height="28"><span class="style32">ชื่อ</span></th>
		<th width="80"><span class="style32">นามสกุล</span></th>
		<th width="80"><span class="style32">ชื่อผู้ใช้</span></th>
        <th width="80"><span class="style32">รหัสผ่าน</span></th>
        <th width="112"><span class="style32">วันที่</span></th>
        <th width="100"><span class="style32">IP Address</span></th>
	  </tr>
	<?
	while($objResult = mysql_fetch_array($objQuery))
	{
	?>
	  <tr>
		<td height="36"  align="center"><?=$objResult["name"];?></td>
		<td  align="center"><?=$objResult["sname"];?></td>
        <td  align="center"><?=$objResult["uname"];?></td>
        <td  align="center"><?=$objResult["upass"];?></td>
        <td  align="center"><?=$objResult["date_re"];?></td>
        <td  align="center"><?=$objResult["ip"];?></td>
	  </tr>
	<?
	}
	?>
	</table>
	<?
	//mysql_close($objConnect);
}
?>
<div align="center"><br>
  <input name="btnExport" type="button" value="นำออก" onClick="JavaScript:window.location='export.php';">
</div>
</body>
</html>
                        
               
               | 
             
            
              
			                
  ประวัติการแก้ไข 2011-05-18 15:28:05              
                              
              
                
                     | 
                     | 
                     | 
                 
                
                     | 
                  
                      
                        | Date :
                            2011-05-18 15:24:28 | 
                        By :
                            NWii | 
                         
                    | 
                     | 
                 
                
                     | 
                     | 
                     | 
                 
                | 
             
           
			         | 
             | 
         
        
             | 
            | 
             | 
             | 
         
          
	    
     
               
		
     
		
	  
        
             | 
            | 
            | 
             | 
         
        
             | 
                       | 
          
            
               
                 Code (PHP) 
<meta http-equiv="content-type" content="text/html; charset=tis-620" />
</head>
<body>
<?
$filName = "vip.csv";
$objWrite = fopen("vip.csv", "w");
include("connect.php");
	
$strSQL = "SELECT * FROM datamem LEFT JOIN dataacc ON (datamem.code = dataacc.code)  WHERE (name LIKE '%".$_SESSION["txtKeyword"]."%' or uname LIKE '%".$_SESSION["txtKeyword"]."%')";
$objQuery = mysql_query($strSQL) or die ("Error Query [".$strSQL."]");
while($objResult = mysql_fetch_array($objQuery))
{
	
	
	fwrite($objWrite, "\"$objResult[name]\",\"$objResult[sname]\",\"$objResult[uname]\",");
	fwrite($objWrite, "\"$objResult[upass]\",\"$objResult[date_re]\",\"$objResult[ip]\" \n");
}
fclose($objWrite);
echo "<script> alert('Download.'); window.location='$filName'; </script>";	
?>
</table>
</body>
</html>
                        
               
               | 
             
            
              
			                
  ประวัติการแก้ไข 2011-05-18 15:27:41              
                              
              
                
                     | 
                     | 
                     | 
                 
                
                     | 
                  
                      
                        | Date :
                            2011-05-18 15:27:09 | 
                        By :
                            NWii | 
                         
                    | 
                     | 
                 
                
                     | 
                     | 
                     | 
                 
                | 
             
           
			         | 
             | 
         
        
             | 
            | 
             | 
             | 
         
          
	    
     
               
		
     
		
	  
        
             | 
            | 
            | 
             | 
         
        
             | 
                       | 
          
            
               
                 ช่วยดูหน่อยนะค่ะ  งงอะ                        
               
               | 
             
            
              
			                              
                              
              
                
                     | 
                     | 
                     | 
                 
                
                     | 
                  
                      
                        | Date :
                            2011-05-18 15:28:53 | 
                        By :
                            NWii | 
                         
                    | 
                     | 
                 
                
                     | 
                     | 
                     | 
                 
                | 
             
           
			         | 
             | 
         
        
             | 
            | 
             | 
             | 
         
          
	    
     
               
		
     
		
	  
        
             | 
            | 
            | 
             | 
         
        
             | 
                       | 
          
            
               
                 ช่วยดูหน่อยนะค่ะ ^^                        
               
               | 
             
            
              
			                              
                              
              
                
                     | 
                     | 
                     | 
                 
                
                     | 
                  
                      
                        | Date :
                            2011-05-19 08:54:54 | 
                        By :
                            NWii | 
                         
                    | 
                     | 
                 
                
                     | 
                     | 
                     | 
                 
                | 
             
           
			         | 
             | 
         
        
             | 
            | 
             | 
             | 
         
          
	    
     
               
		
     
		
	  
        
             | 
            | 
            | 
             | 
         
        
             | 
                       | 
          
            
               
                 Code (PHP) 
<? session_start(); ?>
  
 
ลองใส่ไว้บนสุดละ ทั้ง 2 file                        
               
               | 
             
            
              
			                              
                              
              
                
                     | 
                     | 
                     | 
                 
                
                     | 
                  
                      
                        | Date :
                            2011-05-19 10:34:20 | 
                        By :
                            TEST_CODE | 
                         
                    | 
                     | 
                 
                
                     | 
                     | 
                     | 
                 
                | 
             
           
			         | 
             | 
         
        
             | 
            | 
             | 
             | 
         
          
	    
     
               
		
     
		
	  
        
             | 
            | 
            | 
             | 
         
        
             | 
                       | 
          
            
               
                 ได้แระค่ะ,,,ขอบคุนค่ะ                        
               
               | 
             
            
              
			                              
                              
              
                
                     | 
                     | 
                     | 
                 
                
                     | 
                  
                      
                        | Date :
                            2011-05-19 13:45:49 | 
                        By :
                            NWii | 
                         
                    | 
                     | 
                 
                
                     | 
                     | 
                     | 
                 
                | 
             
           
			         | 
             | 
         
        
             | 
            | 
             | 
             | 
         
          
	    
     
      		  
	
     | 
   
 
                 |