Home >Backend Development >PHP Tutorial >How to automatically generate SQL statements
The fields are relatively simple/If necessary, you can customize the fields and sorting yourself/
There are two levels of city linkage. My business needs it, so there are two levels. There are a lot of information on the three levels on the Internet. You can learn from it if you need it. /
Without further ado, let’s look at the renderings first. The code is below
#
1 <?php 2 3 // 下面三个变量血缘关系比较近, 在同一省份目录中, 只有id是变化的, 其他两个是固定不变的 4 $sort = 31; 5 $id = $sort*100+1; 6 $pid = $sort*100; 7 // $name 是在百度百科搜出来的, 如果格式不一样, 可以自己拆分组合// 建议去百度词条搜, 一般都是下面的格式/ 8 $name = '中西区,东区,南区,湾仔区,九龙城区,观塘区,深水埗区,黄大仙区,油尖旺区 ,离岛区,葵青区,北区,西贡区,沙田区,大埔区,荃湾区,屯门区,元朗区'; 9 $name = explode(',',$name); 10 map($id,$pid,$sort,$name); 11 function map($id,$pid,$sort,$name) 12 { 13 $count = count($name); 14 $str = ''; 15 for($i=0; $i<$count; $i++) 16 { 17 echo "INSERT INTO `xm_official`.`xm_city` (`id`, `parent_id`, `name`, `sort`) VALUES ('".$id."', '".$pid."', '".$name[$i]."', '".$sort."'); "; 18 $id++; 19 } 20 } 21 //访问这个PHP文件, 页面输出的内容就是SQL语句, 直接执行就好了. 22 ?>
The above is the detailed content of How to automatically generate SQL statements. For more information, please follow other related articles on the PHP Chinese website!