  | 
              
	              
	                
  
    
	 
        [สอบถาม] เรื่อง Error Undefined variable: mysqli query ข้อมูลไม่ขึ้น เป็นแค่หน้าเดียว หน้าอื่นที่ใช้ เหมือนกันไม่ขึ้น error ครับ     | 
   
  
    |   | 
   
 
 
 
	
		
			  | 
	   | 
	    | 
		
			  | 
	 
	
		
			  | 
		 		   | 
	  	    
          
            
			
	
			
			 
                คือผมเขียน function auto id ไว้ที่หน้า เชื่อมต่อฐานข้อมูล 
 
แล้วพอตอน เรียกใช้ function auto id ที่เขียนไว้  
 
แล้วมันขึ้น  Error Undefined variable: mysqli ทำให้ข้อมูลดึงไม่ขึ้นครับ 
 
หน้าอื่นๆ ที่ดึงข้อมูลปกติทั่วไป ที่ไม่ใช่ auto id ไม่ error อะไร ข้อมูลมาปกติ  
 
ไม่ทราบว่าต้องแก้ไขตรงไหนครับ ขอบคุณครับ 
 
error ที่เกิดขึ้นครับ 
Code (PHP) 
<br /><b>Notice</b>:  Undefined variable: mysqli in <b>C:\xampp\htdocs\recordlist\include\connectdb.php</b> on line <b>23</b><br /><br /><b>Warning</b>:  mysqli_query() expects parameter 1 to be mysqli, null given in <b>C:\xampp\htdocs\recordlist\include\connectdb.php</b> on line <b>23</b><br /><br /><b>Warning</b>:  mysqli_fetch_assoc() expects parameter 1 to be mysqli_result, null given in <b>C:\xampp\htdocs\recordlist\include\connectdb.php</b> on line <b>24</b><br />B001
  
 
  
 
 
โค๊ดหน้าเชื่อมต่อ ที่เขียน function auto id ไว้ครับ 
 
Code (PHP) 
<?php
$host = "localhost";
$user = "root";
$pass  = "";
$db = "recordlist";
/*$user = "id7337879_root";
$pass  = "12345678";
$db = "id7337879_recordlist";*/
//$mysqli = new mysqli($host,$user,$pass,$db);
$mysqli = mysqli_connect($host,$user,$pass,$db);
mysqli_set_charset($mysqli,"utf8");
//mysql_connect($host,$user,$pass) or die ("Connection fail");
//mysql_select_db($db) or die ("Connection DB fail");
/*
mysql_query("SET NAMES UTF8");
mysql_query("SET charecter_set_results=UTF8");
mysql_query("SET charecter_set_client=UTF8");
mysql_query("set charecter_set_connection=UTF8");*/
function autoidbrand()
{
        $mySQL1 = "select max(brand_id) AS brand_id from tb_brand where brand_id LIKE 'B%'"; // เร้ิมต้น  รัน  ตรงนี้  สำหรับหน้านี้
        $myData1 = mysqli_query($mysqli,$mySQL1); //mydata เก็บค่าใน อาเรย์ ทั้งหมด
        $row1 = mysqli_fetch_assoc($myData1);
        $numcusC = $row1['brand_id'];
        $subC = substr($numcusC, 1); //ตัดตำแหน่งที่ 5 เป็นต้นไป
        $psubC = intval($subC)+1; //แปลง string เป็น Int+เพิ่มอีก 1
        ///////////////////////////////////////////////////////////
        if($psubC == 0){
           $psubC = strval($psubC);	
           $anspsubC =  $numcusC;
        }
        else if($psubC <= 9){
           $psubC = strval($psubC);	
           $anspsubC =  "B00".$psubC;
        }
        else if($psubC <=99){
           $psubC = strval($psubC);	
           $anspsubC =  "B0".$psubC;
        }
        else{
           $psubC = strval($psubC);	
           $anspsubC =  "B".$psubC;
        }
        $id = $anspsubC;
        return $id;
}
function autoidarea()
{
    $mySQL1 = "select max(ar_id) AS ar_id from tb_area where ar_id LIKE 'AR%'"; // เร้ิมต้น  รัน  ตรงนี้  สำหรับหน้านี้/
    $myData1 = mysqli_query($mysqli, $mySQL1); //mydata เก็บค่าใน อาเรย์ ทั้งหมด
    $row1 = mysqli_fetch_assoc($myData1);
    $numcusC = $row1['ar_id'];
    $subC = substr($numcusC, 2); 
    $psubC = intval($subC)+1; //แปลง string เป็น Int+เพิ่มอีก 1
    ///////////////////////////////////////////////////////////
    if($psubC == 0){
       $psubC = strval($psubC);	
       $anspsubC =  $numcusC;
    }
    else if($psubC <= 9){
       $psubC = strval($psubC);	
       $anspsubC =  "AR00".$psubC;
    }
    else if($psubC <=99){
       $psubC = strval($psubC);	
       $anspsubC =  "AR0".$psubC;
    }
    else{
       $psubC = strval($psubC);	
       $anspsubC =  "AR".$psubC;
    }
    $id = $anspsubC;
    return $id;
}
function autoidaff()
{
        $mySQL1 = "select max(aff_id) AS aff_id from tb_affiliation where aff_id LIKE 'A%'"; // เร้ิมต้น  รัน  ตรงนี้  สำหรับหน้านี้
        $myData1 = mysqli_query($mysqli, $mySQL1); //mydata เก็บค่าใน อาเรย์ ทั้งหมด
        $row1 = mysql_fetch_assoc($myData1);
        $numcusC = $row1['aff_id'];
        $subC = substr($numcusC, 1); 
        $psubC = intval($subC)+1; //แปลง string เป็น Int+เพิ่มอีก 1
        ///////////////////////////////////////////////////////////
        if($psubC == 0){
           $psubC = strval($psubC);	
           $anspsubC =  $numcusC;
        }
        else if($psubC <= 9){
           $psubC = strval($psubC);	
           $anspsubC =  "A00".$psubC;
        }
        else if($psubC <=99){
           $psubC = strval($psubC);	
           $anspsubC =  "A0".$psubC;
        }
        else{
           $psubC = strval($psubC);	
           $anspsubC =  "A".$psubC;
        }
        $id = $anspsubC;
        return $id;
}
function autoiddepart()
{
        $mySQL1 = "select max(departID) AS departID from tb_department where departID LIKE 'D%'"; // เร้ิมต้น  รัน  ตรงนี้  สำหรับหน้านี้
        $myData1 = mysqli_query($mysqli, $mySQL1); //mydata เก็บค่าใน อาเรย์ ทั้งหมด
        $row1 = mysqli_fetch_assoc($myData1);
        $numcusC = $row1['departID'];
        $subC = substr($numcusC, 1); 
        $psubC = intval($subC)+1; //แปลง string เป็น Int+เพิ่มอีก 1
        ///////////////////////////////////////////////////////////
        if($psubC == 0){
           $psubC = strval($psubC);	
           $anspsubC =  $numcusC;
        }
        else if($psubC <= 9){
           $psubC = strval($psubC);	
           $anspsubC =  "D000".$psubC;
        }
        else if($psubC <=99){
           $psubC = strval($psubC);	
           $anspsubC =  "D00".$psubC;
        }
        else if($psubC <=999){
                $psubC = strval($psubC);	
                $anspsubC =  "D0".$psubC;
         }
        else{
           $psubC = strval($psubC);	
           $anspsubC =  "D".$psubC;
        }
        $brid = $anspsubC;
        return $id;
}
function autoidposition()
{
        $mySQL1 = "select max(po_id) AS po_id from tb_position where po_id LIKE 'P%'"; // เร้ิมต้น  รัน  ตรงนี้  สำหรับหน้านี้
        $myData1 = mysqli_query($mysqli, $mySQL1); //mydata เก็บค่าใน อาเรย์ ทั้งหมด
        $row1 = mysqli_fetch_assoc($myData1);
        $numcusC = $row1['po_id'];
        $subC = substr($numcusC, 1); 
        $psubC = intval($subC)+1; //แปลง string เป็น Int+เพิ่มอีก 1
        ///////////////////////////////////////////////////////////
        if($psubC == 0){
           $psubC = strval($psubC);	
           $anspsubC =  $numcusC;
        }
        else if($psubC <= 9){
           $psubC = strval($psubC);	
           $anspsubC =  "P000".$psubC;
        }
        else if($psubC <=99){
           $psubC = strval($psubC);	
           $anspsubC =  "P00".$psubC;
        }
        else if($psubC <=999){
                $psubC = strval($psubC);	
                $anspsubC =  "P0".$psubC;
         }
        else{
           $psubC = strval($psubC);	
           $anspsubC =  "P".$psubC;
        }
        $id = $anspsubC;
        return $id;
}
?>
 
 
  Tag : PHP, MySQL               
                        | 
           
          
            | 
			
                             | 
           
          
            
              
                   | 
                   | 
                   | 
               
              
                   | 
                
                    
                      | Date :
                          2018-10-02 14:24:35 | 
                      By :
                          nutzaaclub | 
                      View :
                          3950 | 
                      Reply :
                          2 | 
                     
                  | 
                   | 
               
              
                   | 
                   | 
                   | 
               
              | 
           
          
            | 
			 | 
           
         
	    
		             | 
		
			  | 
	 
	
		
			  | 
		  | 
		
			  | 
		
			  | 
	 
 
              
  
          
		
     
		
	  
        
             | 
            | 
            | 
             | 
         
        
             | 
                       | 
          
            
               
                 มันเกิดขึ้นเพราะว่า ใน function มันไม่รู้จักตัวแปร ข้างนอก function ครับ 
 
Code (PHP) 
$a = 10;
function calculate()
{
  echo $a;
}
calculate();
 
 
จะเห็นว่าผมประกาศตัวแปรใว้นอก function calculate แต่ภายในมีการเรียกใช้ตัวแปร $a มันเลยไม่รู้จัก ก็จะ เกิด error  
วิธีแก้ก็เขียนให้เป็น class 
 
Code (PHP) 
class A {
   private $a = 10;
	
   function calcualte()
   {
	   echo $this->a;
   }	
}
$a = new A();
$a->calcualte();
                        
               
               | 
             
            
              
			                              
                              
              
                
                     | 
                     | 
                     | 
                 
                
                     | 
                  
                      
                        | Date :
                            2018-10-02 15:03:23 | 
                        By :
                            DK | 
                         
                    | 
                     | 
                 
                
                     | 
                     | 
                     | 
                 
                | 
             
           
			         | 
             | 
         
        
             | 
            | 
             | 
             | 
         
          
	    
     
               
		
     
		
	  
        
             | 
            | 
            | 
             | 
         
        
             | 
                       | 
          
            
               
                 ตาม 1 ถูกต้องแล้วครับ เปลี่ยนวิธีการเขียน 
 
แต่ถ้าจะเอาเร็วๆ รีบๆ ก็แบบนี้ 
 
Code (PHP) 
$mysqli = new mysqli('host', 'user', 'pswd', 'dbname') die('mysqli connection error');
function mycon(){
   global $mysqli; 
   return $mysqli;
}
// เวลาเรียกใช้ ตรงไหนก็ได้
function other(){
   mycon()->query('dafjsdjasd') or die( mycon()->error);
}
                        
               
               | 
             
            
              
			                
  ประวัติการแก้ไข 2018-10-02 16:46:30              
                              
              
                
                     | 
                     | 
                     | 
                 
                
                     | 
                  
                      
                        | Date :
                            2018-10-02 16:44:22 | 
                        By :
                            Chaidhanan | 
                         
                    | 
                     | 
                 
                
                     | 
                     | 
                     | 
                 
                | 
             
           
			         | 
             | 
         
        
             | 
            | 
             | 
             | 
         
          
	    
     
      		  
	
     | 
   
 
                 |