  | 
              
	              
	                
  
    
	 
        สอบถามเรื่องการเขียนฟังก์ชั่นใน PHP + MySQLi with Prepared Statements หน่อยครับ     | 
   
  
    |   | 
   
 
 
 
	
		
			  | 
	   | 
	    | 
		
			  | 
	 
	
		
			  | 
		 		   | 
	  	    
          
            
			
	
			
			 
                รบกวนดูฟังก์ชันให้หน่อยครับ ไม่แน่ใจว่าผมเขียนผิดตรงไหนบ้างครับ พอเรียกใช้ฟังก์ชันแล้วไม่แสดงค่าอะไรเลยครับ 
 
Code (PHP) 
$DBServer = 'localhost'; 
$DBUser   = 'xxxx';
$DBPass   = 'xxxx';
$DBName   = 'xxxx';
$conn_mysqli = new mysqli($DBServer, $DBUser, $DBPass, $DBName);
/* check connection */
if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
}
/* change character set to utf8 */
$conn_mysqli->set_charset("utf8");
function get_years($sysid){
		//$sys_ID = 1;
		$stmts = $conn_mysqli->prepare("SELECT sys_Value FROM SystemConfig WHERE sys_ID = ?");
		$stmts->bind_param('i', $sysid);
		$stmts->execute();
		$stmts->bind_result($sys_Value);
		$stmts->fetch();
		$stmts->close();
		return $sys_Value;
}
echo get_years(1);
 
 
  Tag : PHP, MySQL, Apache, Appserv, XAMPP, Windows               
                        | 
           
          
            | 
			
                             | 
           
          
            
              
                   | 
                   | 
                   | 
               
              
                   | 
                
                    
                      | Date :
                          2018-05-25 12:59:19 | 
                      By :
                          Attapornn | 
                      View :
                          836 | 
                      Reply :
                          7 | 
                     
                  | 
                   | 
               
              
                   | 
                   | 
                   | 
               
              | 
           
          
            | 
			 | 
           
         
	    
		             | 
		
			  | 
	 
	
		
			  | 
		  | 
		
			  | 
		
			  | 
	 
 
              
  
          
		
     
		
	  
        
             | 
            | 
            | 
             | 
         
        
             | 
                       | 
          
            
               
                 return ผิดตัวมั้ยครับ 
 
Code (PHP) 
function get_years($sysid){
		//$sys_ID = 1;
		$stmts = $conn_mysqli->prepare("SELECT sys_Value FROM SystemConfig WHERE sys_ID = ?");
		$stmts->bind_param('i', $sysid);
		$stmts->execute();
		$stmts->bind_result($sys_Value);
	        $data = $stmts->fetch();
		$stmts->close();
		return $data;
}
                        
               
               | 
             
            
              
			                              
                              
              
                
                     | 
                     | 
                     | 
                 
                
                     | 
                  
                      
                        | Date :
                            2018-05-25 13:22:06 | 
                        By :
                            mongkon.k | 
                         
                    | 
                     | 
                 
                
                     | 
                     | 
                     | 
                 
                | 
             
           
			         | 
             | 
         
        
             | 
            | 
             | 
             | 
         
          
	    
     
               
		
     
		
	  
        
             | 
            | 
            | 
             | 
         
        
             | 
                       | 
          
            
               
                 Code (PHP) 
function get_years($sysid){
   global $conn_mysqli;
 
 
หรือ 
Code (PHP) 
function get_years($sysid, $conn_mysqli){
                        
               
               | 
             
            
              
			                              
                              
              
                
                     | 
                     | 
                     | 
                 
                
                     | 
                  
                      
                        | Date :
                            2018-05-25 13:28:36 | 
                        By :
                            Chaidhanan | 
                         
                    | 
                     | 
                 
                
                     | 
                     | 
                     | 
                 
                | 
             
           
			         | 
             | 
         
        
             | 
            | 
             | 
             | 
         
          
	    
     
               
		
     
		
	     
	    
     
               
		
     
		
	     
	    
     
               
		
     
		
	     
	    
     
               
		
     
		
	  
        
             | 
            | 
            | 
             | 
         
        
             | 
                       | 
          
            
               
                 ตัวแปร นอก function ไม่สามารถเรียกใช้งานได้ ถ้าไม่กำหนดให้เป็น ตัวแปรชนิด global 
 
$conn_mysqli ถูกสร้าง นอก function  
ดังนั้นต้องบอกให้ function กำหนดตัวแปรนี้เป็น ชนิด global 
แต่การกำหนด global ภายใน function ก็จะใช้งานได้เฉพาะภายใน function นั้นเท่านั้นเอง 
ถ้าอยากจะให้เรียกใช้งานได้ทั้ง โปรแกรมทุกๆ ที่ ก็ต้องกำหนด global ไว้นอกสุดที่เดียว 
หรือทำเป็น function ไว้อ้างอิง 
 
Code (PHP) 
$conx = new mysqli( ..... );
function mydb (){
   global $conx;
   return $conx;
}
function new_fnc(){
  $rs = mydb()->prepare('select  * from tablename where id=?');
}
 
ปล. 
โค๊ดข้างบนเป็นการอธิบายเพื่อให้เข้าใจ ไวยกรณ์ของ php เท่านั้นครับ ถ้าอยากให้ใช้ได้ ทุก function ทุกคลาสจริง 
ก็ใช้  
Code (PHP) 
<?php
global $variable;
$variable = 1234; 
  
ที่นี้ก็ใช้ตัวแปร $variable; นี่ได้ทุกที่ในโปรแกรมแล้วล่ะครับ                        
               
               | 
             
            
              
			                
  ประวัติการแก้ไข 2018-05-25 16:48:17 2018-05-25 16:49:33 2018-05-25 16:51:19 2018-05-25 16:56:12              
                              
              
                
                     | 
                     | 
                     | 
                 
                
                     | 
                  
                      
                        | Date :
                            2018-05-25 16:47:22 | 
                        By :
                            Chaidhanan | 
                         
                    | 
                     | 
                 
                
                     | 
                     | 
                     | 
                 
                | 
             
           
			         | 
             | 
         
        
             | 
            | 
             | 
             | 
         
          
	    
     
               
		
     
		
	     
	    
     
      		  
	
     | 
   
 
                 |