ฟังก์ชันนี้สร้างขึ้น เพื่ออำนวยความสะดวก สำหรับคนที่ต้อง
เขียนโปรแกรม เชิ่อมต่อกับฐานข้อมูลบ่อยๆ
เริ่มต้นให้เราสร้างไฟล์ func_gensql.php และกำหนดโค้ดต่อไปนี้
ไฟล์ func_gensql.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 | <?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
1 2 3 4 5 6 | <?php // โค้ดไฟล์ dbconnect.php ดูได้ที่ http://niik.in/que_2398_5642 require_once ( "dbconnect.php" ); include_once ( "func_gensql.php" ); ?> |
ตัวอย่างตาราง ประกอบคำอธิบาย
1 2 3 4 5 6 7 | 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 ตัวอย่างผลลัพธ์ที่ได้
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | <?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 ผลลัพธ์ที่แสดง
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | 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 ']."' |
ตัวอย่างการเรียกใช้
1 | // gen_sql("test_salary","t_id","_GET['ddd']",true,null,true); |
ชุดคำสั่ง sql ผลลัพธ์ที่แสดง
1 2 3 4 5 6 7 8 9 | 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 ']."' |