ฟังก์ชั่น php เรียกใช้งาน สร้าง คำสั่ง sql อย่างง่าย

เขียนเมื่อ 9 ปีก่อน โดย Ninenik Narkdee
php sql

คำสั่ง การ กำหนด รูปแบบ ตัวอย่าง เทคนิค ลูกเล่น การประยุกต์ การใช้งาน เกี่ยวกับ php sql





ฟังก์ชันนี้สร้างขึ้น เพื่ออำนวยความสะดวก สำหรับคนที่ต้อง
เขียนโปรแกรม เชิ่อมต่อกับฐานข้อมูลบ่อยๆ 
เริ่มต้นให้เราสร้างไฟล์ func_gensql.php และกำหนดโค้ดต่อไปนี้
 

ไฟล์ func_gensql.php

 
<?php
function gen_sql($table,$where="",$type,$act_update=true,$act_insert=true,$act_delete=true){
	global $mysqli;
    $result = $mysqli->query("SHOW COLUMNS FROM $table");
    if(!$result){
        echo 'Could not run query: ' . $mysqli->error;
        exit;
    }
    $g_type=$type;
    $g_type2=$type;
    $g_type3=$type;
    $arr_table_field=array();
    $arr_table_type=array();
    if($result->num_rows>0) {
        while ($row = $result->fetch_assoc()) {
            if(count($row)>0){
                foreach($row as $k_field=>$v_field){
                    if($k_field=="Field"){
                        $arr_table_field[]=$v_field;
                    }
                    if($k_field=="Type"){
                        $arr_table_type[]=$v_field;
                    }
                }
            }
        }
        if($act_update){
            $str_update="<pre>UPDATE $table SET "."<br>";
            $num_field=count($arr_table_field);
            $i_field=0;
            if($num_field>0){
                foreach($arr_table_field  as $k_field=>$v_field){
                    $i_field++;
                    if(preg_match('/(int|tinyint|smallint|mediumint|bigint|float|double|decimal)/', $arr_table_type[$k_field], $matches, PREG_OFFSET_CAPTURE)){
                        $type="trim($".$g_type.")";
                    }else   if(preg_match('/(char|varchar|longvarchar|tinytext|text|mediumtext|longtext)/', $arr_table_type[$k_field], $matches, PREG_OFFSET_CAPTURE)){
                        $type="addslashes($".$g_type.")";
                    }else   if(preg_match('/^date/', $arr_table_type[$k_field], $matches, PREG_OFFSET_CAPTURE)){
                        $type="date('Y-m-d')";
                    }else   if(preg_match('/^time/', $arr_table_type[$k_field], $matches, PREG_OFFSET_CAPTURE)){
                        $type="date('H:i:s')";												
                    }else   if(preg_match('/^datetime/', $arr_table_type[$k_field], $matches, PREG_OFFSET_CAPTURE)){
                        $type="date('Y-m-d H:i:s')";
                    }else{
                        $type="$$g_type";
                    }
                    if($i_field==$num_field){
                        $str_update.="   ".$v_field."='".$type."'"."<br>";
                    }else{
                        $str_update.="   ".$v_field."='".$type."',"."<br>";
                    }
                }
                $str_update.="WHERE $where='".$$g_type."'"."<br></pre>";
            }
            echo $str_update;
        }  
        if($act_insert){
            echo "<br><br><pre>";
            $str_insert="INSERT INTO $table (<br>";   
            $num_field=count($arr_table_field);
            $i_field=0;
            if($num_field>0){
                $str_insert_field="";
                $str_insert_value="";
                foreach($arr_table_field  as $k_field=>$v_field){
                    $i_field++;
                    if(preg_match('/(int|tinyint|smallint|mediumint|bigint|float|double|decimal)/', $arr_table_type[$k_field], $matches, PREG_OFFSET_CAPTURE)){
                        $type="trim($".$g_type2.")";
                    }else   if(preg_match('/(char|varchar|longvarchar|tinytext|text|mediumtext|longtext)/', $arr_table_type[$k_field], $matches, PREG_OFFSET_CAPTURE)){
                        $type="addslashes($".$g_type2.")";
                    }else   if(preg_match('/^date/', $arr_table_type[$k_field], $matches, PREG_OFFSET_CAPTURE)){
                        $type="date('Y-m-d')";
                    }else   if(preg_match('/^time/', $arr_table_type[$k_field], $matches, PREG_OFFSET_CAPTURE)){
                        $type="date('H:i:s')";												
                    }else   if(preg_match('/^datetime/', $arr_table_type[$k_field], $matches, PREG_OFFSET_CAPTURE)){
                        $type="date('Y-m-d H:i:s')";
                    }else{
                        $type="$$g_type2";
                    }
                    if($i_field==$num_field){
                        $str_insert_field.="   ".$v_field."<br>";
                        $str_insert_value.="   '".$type."'"."<br>";
                    }else{
                        $str_insert_field.="   ".$v_field.",<br>";
                        $str_insert_value.="   '".$type."',"."<br>";
                    }
                }
                $str_insert.=$str_insert_field;
                $str_insert.=") VALUES (<br>";
                $str_insert.=$str_insert_value;
                $str_insert.=")<br></pre>";
            }       
            echo $str_insert;
        }         
        if($act_delete){
            echo "<br><br><pre>";
            $str_delete="DELETE FROM $table WHERE $where='".$$g_type3."'"."<br></pre>";
            echo $str_delete;
        }
    }   
}
 
จากนั้นเรียกใช้ผ่านการ include เข้ามา ในที่นี้ เราจะใช้กับ mysqli 
 

ไฟล์ demo.php

 
<?php
// โค้ดไฟล์ dbconnect.php ดูได้ที่ http://niik.in/que_2398_5642
require_once("dbconnect.php");
include_once("func_gensql.php");

?>
 

ตัวอย่างตาราง ประกอบคำอธิบาย

 
CREATE TABLE `test_salary` (
  `t_id` int(11) NOT NULL auto_increment,
  `t_name` varchar(10) NOT NULL,
  `t_position` int(11) NOT NULL,
  `t_salary` int(11) NOT NULL,
  PRIMARY KEY  (`t_id`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=6 ;
 

การใช้งาน

    ฟังก์ชั่นข้างต้น จะทำการสร้างรูปแบบคำสั่ง sql ซึ่งประกอบไปด้วย
UPDATE INSERT และ DELETE ตัวอย่างผลลัพธ์ที่ได้
 
 <?php
// โค้ดไฟล์ dbconnect.php ดูได้ที่ http://niik.in/que_2398_5642
require_once("dbconnect.php");
include_once("func_gensql.php");

/*
gen_sql("ชื่อตาราง",
"ฟิลด์หลัก ที่ต้องการ เช่น primary key",
"ค่าตัวแปรที่ต้องการ เบื้องต้น_POST['ddd'] ",
"แสดง UPDATE ค่า true / null ค่าเริ่มต้น true ",
"แสดง INSERT ค่า true / null ค่าเริ่มต้น true ",
"แสดง DELETE ค่า true / null ค่าเริ่มต้น true ");
*/

gen_sql("test_salary","t_id","_POST['ddd']");
// หรือ gen_sql("test_salary","t_id","_POST['ddd']",true,true,true);

?>
 
ชุดคำสั่ง sql ผลลัพธ์ที่แสดง
 
UPDATE test_salary SET
   t_id='".trim($_POST['dddd'])."',
   t_name='".addslashes($_POST['dddd'])."',
   t_position='".trim($_POST['dddd'])."',
   t_salary='".trim($_POST['dddd'])."'
WHERE t_id='".$_POST['dddd']."'
 
 
INSERT INTO test_salary (
   t_id,
   t_name,
   t_position,
   t_salary
) VALUES (
   '".trim($_POST['dddd'])."',
   '".addslashes($_POST['dddd'])."',
   '".trim($_POST['dddd'])."',
   '".trim($_POST['dddd'])."'
)
 
 
DELETE FROM test_salary WHERE t_id='".$_POST['dddd']."'
 
ตัวอย่างการเรียกใช้
 
// gen_sql("test_salary","t_id","_GET['ddd']",true,null,true);
 
ชุดคำสั่ง sql ผลลัพธ์ที่แสดง
 
UPDATE test_salary SET
   t_id='".trim($_GET['dddd'])."',
   t_name='".addslashes($_GET['dddd'])."',
   t_position='".trim($_GET['dddd'])."',
   t_salary='".trim($_GET['dddd'])."'
WHERE t_id='".$_GET['dddd']."'
 
 
DELETE FROM test_salary WHERE t_id='".$_GET['dddd']."'
 


กด Like หรือ Share เป็นกำลังใจ ให้มีบทความใหม่ๆ เรื่อยๆ น่ะครับ





Tags:: sql php







URL สำหรับอ้างอิง











เว็บไซต์ของเราให้บริการเนื้อหาบทความสำหรับนักพัฒนา โดยพึ่งพารายได้เล็กน้อยจากการแสดงโฆษณา โปรดสนับสนุนเว็บไซต์ของเราด้วยการปิดการใช้งานตัวปิดกั้นโฆษณา (Disable Ads Blocker) ขอบคุณครับ