首页 | 注册 | 登录 | 搜索 | 标签 | 关于

MYSQL语句生成器

  1. <?php   
  2. /*   
  3. * mysql语句生成器  
  4. * 创建时间:2005-01-11  
  5. * 作  者:多菜鸟  
  6. * 来  源: http://blog.csdn.net/kinge...  
  7. * 联系邮箱: kingerq AT msn DOT com  
  8. */  
  9. session_start();   
  10. $code = array();   
  11. $line = array("=================插入语句=================",    
  12.   "=================选择语句=================",    
  13.   "=================更新语句=================");   
  14. $js = "";   
  15. $typein = array();   
  16. $tbl_list = $tbltemp = "";   
  17.   
  18. if($_POST){   
  19. if(array_key_exists("tbl_name"$_POST)){   
  20.  $tbltemp = $_POST["tbl_name"];   
  21.  $_POST = $_SESSION["phpcode"][$_POST["tbl_name"]];   
  22.  //print_r($_SESSION["phpcode"]);   
  23. }else{   
  24.  $_SESSION["phpcode"][$_POST["tbl"]] = $_POST;   
  25. }   
  26. $code[0] = createcode($_POST, 0);//插入语句   
  27. $code[1] = createcode($_POST, 1);//选择语句   
  28. $code[2] = createcode($_POST, 2);//更新语句   
  29. $typein = $_POST["outtype"];   
  30.   
  31. foreach($typein as $val){   
  32.  $js .= "code[\"outtype[]\"][".$val."].checked = true;\n";   
  33. }   
  34. echo "<script>  
  35. window.onload = function(){  
  36.  code.tbl.value = '".$_POST["tbl"]."';  
  37.  code.fieldlist.value = '".$_POST["fieldlist"]."';  
  38.  code.keyword.value = '".$_POST["keyword"]."';  
  39.  code.codetype[".$_POST["codetype"]."].checked = true;  
  40.  ".$js."  
  41. }  
  42. </script>";   
  43. }   
  44. //创建历史下拉菜单   
  45. if(!emptyempty($_SESSION["phpcode"])){   
  46. foreach(array_filter(array_reverse($_SESSION["phpcode"])) as $key => $val){   
  47.  $tbl_list .= "<option value='".$key."'".($tbltemp == $key ? " selected" : "").">".$key."</option>";   
  48. }   
  49. }   
  50.   
  51. /*  
  52. * 生成语句函数  
  53. */  
  54. function createcode($ar$type = 0){   
  55. switch($type){   
  56.  case 1://选择语句处理   
  57.   $sql = "\$sql = \"SELECT ".fieldlist($ar["fieldlist"])." \nFROM `".$ar["tbl"]."` ".where($_POST["keyword"])."\";\n";   
  58.      
  59.   break;   
  60.  case 2://更新语句处理   
  61.   $sql = "\$sql = \"UPDATE `".$ar["tbl"]."` SET ".fieldlist($ar["fieldlist"], 2)." ".where($_POST["keyword"])." LIMIT 1\";\n";   
  62.      
  63.   break;   
  64.  default://插入语句处理   
  65.   $sql = "\$sql = \"INSERT INTO `".$ar["tbl"]."`(".fieldlist($ar["fieldlist"]).")\nVALUES(".fieldlist($ar["fieldlist"], 1).")\";\n";   
  66.      
  67. }   
  68. if($ar["codetype"]){//输出PHPLIB   
  69.  $sql  = "include(\"inc/db_mysql.inc\");//包括MYSQL操作类\n\$db = new DB_Sql;//定义类\n\$db->connect();//连接数据库\n\n".$sql;   
  70.  $sql .= "\$db->query(\$sql);\n";   
  71. }else{   
  72.  $sql = "/* 连接数据库 */\n\$conn = mysql_pconnect(\"localhost\", \"username\", \"password\") or die(\"不能连接数据库:\".mysql_error());\n/* 选择数据库 */\nmysql_select_db(\"mysql_database\") or die(\"不能选择数据库\");\n\n".$sql;   
  73.  $sql .= "\$result = mysql_query(\$sql);\n";   
  74. }   
  75. return $sql;   
  76. }   
  77. /*  
  78. * 字段处理  
  79. */  
  80. function fieldlist($fieldstr$type = 0){   
  81. $str = "";   
  82. $tstr = split(","$fieldstr);   
  83. foreach($tstr as $val){   
  84.  switch($type){   
  85.   case 1://用在插入语句中   
  86.    if($str$str .= ", ";    
  87.    $str .= "'\".\$_POST[\"".trim($val)."\"].\"'";    
  88.    break;   
  89.   case 2://用在更新语句中   
  90.    if($str$str .= ", \n";    
  91.    $str .= "`".trim($val)."` = '\".\$_POST[\"".trim($val)."\"].\"'";   
  92.    break;   
  93.   default:   
  94.    if($str$str .= ", ";    
  95.    $str .= "`".trim($val)."`";    
  96.  }   
  97. }   
  98. return $str;   
  99. }   
  100.   
  101. function where($keyword){   
  102. return "\nWHERE `$keyword` = '\".\$_POST[\"".$keyword."\"].\"'";   
  103. }   
  104. ?>   
  105. <style type="text/css">   
  106. <!--   
  107. .border {   
  108. border: 1px dashed #009E6B;   
  109. }   
  110. td,body {   
  111. font-size: 12px;   
  112. }   
  113. a {   
  114. font-size: 12px;   
  115. color: #FFFFFF;   
  116. text-decoration: none;   
  117. }   
  118. a:hover {   
  119. font-size: 12px;   
  120. color: #000000;   
  121. text-decoration: underline;   
  122. }   
  123. -->   
  124. </style>   
  125.   
  126. <title>MYSQL语句生成器</title><table width="60%"  border="0" align="center" cellpadding="4" cellspacing="0">   
  127.  <tr>   
  128.    <td bgcolor="#009F6B"><a href="http://blog.csdn.net/kinge...返回</a></td>  
  129.    <form name="his" method="post" action="">  
  130. <td width="25" bgcolor="#009F6B">  
  131.      <?  
  132.   if($tbl_list){  
  133.   ?>  
  134.   <select name="tbl_name" onChange="his.submit()">  
  135.   <?= $tbl_list?>  
  136.      </select>  
  137.      <?  
  138.   }  
  139.   ?>  
  140.    </td>  
  141. </form>  
  142.  </tr>  
  143. </table>  
  144.  <table width="60%"  border="0" align="center" cellpadding="2" cellspacing="1" class="border">  
  145. <form name="code" method="post" action="" onSubmit="return chkform()">  
  146. <script language="JavaScript">  
  147. <!--  
  148. function chkform(){  
  149. var reg = /^[^\d]\w*$/;  
  150. if(!reg.test(code.tbl.value)){  
  151.  alert("请输入正确的表名");  
  152.  code.tbl.focus();  
  153.  return false;  
  154. }  
  155. var field = code.fieldlist.value.split(",");  
  156. for(i = 0; i < field.length; i++){  
  157.  if(!reg.test(field[i])){  
  158.   alert("请输入正确的字段列表\n\n不能以数字为开头的,\n用逗号分开各字段");  
  159.   code.fieldlist.focus();  
  160.   return false;  
  161.  }  
  162. }  
  163. if(!reg.test(code.keyword.value)){  
  164.  alert("请输入正确的主键字段");  
  165.  code.keyword.focus();  
  166.  return false;  
  167. }  
  168. }  
  169. //-->  
  170. </script>   
  171.    <tr>  
  172.      <td align="right" nowrap>表名:</td>  
  173.      <td><input name="tbl" type="text" id="tbl"></td>  
  174.    </tr>  
  175.    <tr>  
  176.      <td align="right" nowrap>字段列表:</td>  
  177.      <td><textarea name="fieldlist" cols="45" rows="7" id="fieldlist"></textarea>  
  178.      <br>  
  179.      用逗号(,)分开各字段</td>  
  180.    </tr>  
  181.    <tr>  
  182.      <td align="right" nowrap>主键字段:</td>  
  183.      <td><input name="keyword" type="text" id="keyword"></td>  
  184.    </tr>  
  185.    <tr>  
  186.      <td align="right" nowrap>代码类型:</td>  
  187.      <td><input name="codetype" type="radio" value="0" checked>  
  188.        一般代码  
  189.        <input type="radio" name="codetype" value="1">  
  190.      PHPLIB</td>  
  191.    </tr>  
  192.    <tr>  
  193.      <td align="right" nowrap>输出类型:</td>  
  194.      <td><input name="outtype[]" type="checkbox" id="outtype[]" value="0" checked>  
  195.        插入  
  196.          <input name="outtype[]" type="checkbox" id="outtype[]" value="1" checked>  
  197.          选择  
  198.          <input name="outtype[]" type="checkbox" id="outtype[]" value="2" checked>  
  199.          更新</td>  
  200.    </tr>  
  201.    <tr>  
  202.      <td align="right"> </td>  
  203.      <td><input type="submit" name="Submit" value="生成代码">  
  204.      <input type="reset" name="Submit" value="重置"></td>  
  205.    </tr>  
  206. </form>  
  207. </table>  
  208.  <br>  
  209. <table width="70%"  border="0" align="center" cellpadding="5" cellspacing="1" class="border">  
  210.  <tr>  
  211.    <td bgcolor="#CCCCCC">  
  212. <?php  
  213. foreach($code as $key=>$val){  
  214. if(in_array($key, $typein)) {  
  215.  echo $line[$key]."<br>\n ";  
  216.  highlight_string("<?php\n".$val."\n?>\n");   
  217. }   
  218. }   
  219. ?></td>   
  220.  </tr>   
  221. </table>  

上一篇 :刚才一个朋友发来一短信
下一篇 :js右键菜单

本文链接:http://www.amxku.net/archives/467/

发表评论

评论内容: