  | 
              
	              
	                
  
    
	 
        ต้องการดึงค่าจาก MySQL มาเป็น Array แบบตัวอย่างครับ ไม่ทราบว่าต้องทำยังไงครับ     | 
   
  
    |   | 
   
 
 
 
              
  
          
		
     
		
	     
	    
     
               
		
     
		
	     
	    
     
               
		
     
		
	  
        
             | 
            | 
            | 
             | 
         
        
             | 
                       | 
          
            
               
                 Code (PHP) 
<?
$strSql="select * from table ";
$query = mysql_query($strSql);
$num = mysql_num_rows($query);
$resultArray = array();
for ($i = 0;$i<$num;$i++) {
	$result = mysql_fetch_array($query);
	array_push($resultArray,$result);
}
?>
 
 
จะได้ $resultArray เป็น array ครับ                        
               
               | 
             
            
              
			  			  
			                              
                              
              
                
                     | 
                     | 
                     | 
                 
                
                     | 
                  
                      
                        | Date :
                            2010-12-03 16:27:06 | 
                        By :
                            webmaster | 
                         
                    | 
                     | 
                 
                
                     | 
                     | 
                     | 
                 
                | 
             
           
			         | 
             | 
         
        
             | 
            | 
             | 
             | 
         
          
	    
     
               
		
     
		
	  
        
             | 
            | 
            | 
             | 
         
        
             | 
                       | 
          
            
               
                 ขอบคุณครับ แต่ยังไม่ได้งงมากครับ  
เป้าหมายของผมนะครับ ผมจะตรวจสอบค่าซ้ำก่อนบันทึกโดยใช้ AJAX ครับ จาก code นะครับ 
1.index.html 
2.jquery.js 
3.user_availability.php 
 
index.html นะคับCode (PHP) 
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Fancy Style Username availability Checking using jQuery</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<script src="jquery.js" type="text/javascript" language="javascript"></script>
<script language="javascript">
//<!---------------------------------+
//  Developed by Roshan Bhattarai 
//  Visit http://roshanbh.com.np for this script and more.
//  This notice MUST stay intact for legal use
// --------------------------------->
$(document).ready(function()
{
	$("#username").blur(function()
	{
		//remove all the class add the messagebox classes and start fading
		$("#msgbox").removeClass().addClass('messagebox').text('Checking...').fadeIn("slow");
		//check the username exists or not from ajax
		$.post("user_availability.php",{ user_name:$(this).val() } ,function(data)
        {
		  if(data=='no') //if username not avaiable
		  {
		  	$("#msgbox").fadeTo(200,0.1,function() //start fading the messagebox
			{ 
			  //add message and change the class of the box and start fading
			  $(this).html('This User name Already exists').addClass('messageboxerror').fadeTo(900,1);
			});		
          }
		  else
		  {
		  	$("#msgbox").fadeTo(200,0.1,function()  //start fading the messagebox
			{ 
			  //add message and change the class of the box and start fading
			  $(this).html('Username available to register').addClass('messageboxok').fadeTo(900,1);	
			});
		  }
				
        });
 
	});
});
</script>
</style>
</head>
<body>
<br>
<br>
<div align="center">
<div class="top" > A fancy username availability checker using jQuery - <strong>Username "roshan", "mike" ,"jason" already exists</strong><br>
  Please move the focus out of the box to check the availability of username
</div>
<div >
   User Name : <input name="username" type="text" id="username" value="" maxlength="15" />
   <span id="msgbox" style="display:none"></span>
</div>
</div>
</body>
</html>
 
 
user_availability.php 
<?php
//  Developed by Roshan Bhattarai 
//  Visit http://roshanbh.com.np for this script and more.
//  This notice MUST stay intact for legal use
//this varible contains the array of existing users
	header("Content-Type:text/plain;charset=TIS-620");
	$objConnect = mysql_connect("localhost","root","admin") or die("Error Connect to Database");
	$objDB = mysql_select_db("abc");
	//*** Check Username (already exists) ***//
		$strSql="select * from account ";
		$query = mysql_query($strSql);
		$num = mysql_num_rows($query);
		$resultArray = array();
		for ($i = 0;$i<$num;$i++) {
			$result = mysql_fetch_array($query);
			array_push($resultArray,$result); 		
				
		}
	//$existing_users=array('roshan','mike','jason','อา.');  [font color='red']ถ้าเป็นแบบนี้อ่ะได้ครับ เพราะเป็น array[/font]
$existing_users=($result); 
//value got from the get metho
$user_name=$_POST['user_name'];
//checking weather user exists or not in $existing_users array
if (in_array($user_name, $existing_users))
{
	//user name is not availble
	echo "no";
} 
else
{
	//user name is available
	echo "yes";
}
?>
 
แล้วอยากได้ฐานข้อมูลจาก Mysql มาเป็น array โดยที่  
record 1 =  array ตัวที่ 1 
record 2 = array ตัวที่ 2 ประมาณนี้อ่ะครับ                        
               
               | 
             
            
              
			                
  ประวัติการแก้ไข 2010-12-03 16:43:03              
                              
              
                
                     | 
                     | 
                     | 
                 
                
                     | 
                  
                      
                        | Date :
                            2010-12-03 16:39:30 | 
                        By :
                            tongmew02 | 
                         
                    | 
                     | 
                 
                
                     | 
                     | 
                     | 
                 
                | 
             
           
			         | 
             | 
         
        
             | 
            | 
             | 
             | 
         
          
	    
     
               
		
     
		
	     
	    
     
               
		
     
		
	     
	    
     
               
		
     
		
	     
	    
     
               
		
     
		
	  
        
             | 
            | 
            | 
             | 
         
        
             | 
                       | 
          
            
               
                 เขาต้องการคำตอบแค่นี้แหละมั้ง 
 
Code (PHP) 
$sql = "//คำสั่ง query ที่ต้องการจะเอาค่า";
$result = mysql_query($sql,$con);
while($a= mysql_fetch_array($result)){
	      print_r($a); //แสดงผลเพื่อดูผลลัพท์
}
 
 
 
ผลการ run จะได้ตัวแปลที่ต้องการทั้งหมดแต่ละบรรทัด เก็บเป็น Array ใส่ในตัวแปล $a                        
               
               | 
             
            
              
			                              
                              
              
                
                     | 
                     | 
                     | 
                 
                
                     | 
                  
                      
                        | Date :
                            2015-02-06 10:53:43 | 
                        By :
                            www.fb.com/chatchai.h2o | 
                         
                    | 
                     | 
                 
                
                     | 
                     | 
                     | 
                 
                | 
             
           
			         | 
             | 
         
        
             | 
            | 
             | 
             | 
         
          
	    
     
      		  
	
     | 
   
 
                 |