Home >Backend Development >PHP Tutorial >Database dictionary generation

Database dictionary generation

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOriginal
2016-07-25 08:48:011375browse
PHP generates data dictionary for mysql database
  1. /**
  2. * Generate mysql data dictionary
  3. */
  4. header("Content-type: text/html; charset=utf-8");
  5. //Configure database
  6. $dbserver = "127.0.0.1 ";
  7. $dbusername = "root";
  8. $dbpassword = "";
  9. $database = "think_team";
  10. //Other configurations
  11. $mysql_conn = @mysql_connect("$dbserver", "$dbusername", "$ dbpassword") or die("Mysql connect is error.");
  12. mysql_select_db($database, $mysql_conn);
  13. mysql_query('SET NAMES utf8', $mysql_conn);
  14. $table_result = mysql_query('show tables', $ mysql_conn);
  15. $no_show_table = array(); //Tables that do not need to be displayed
  16. $no_show_field = array(); //Fields that do not need to be displayed
  17. //Get all table names
  18. while($row = mysql_fetch_array ($table_result)){
  19. if(!in_array($row[0],$no_show_table)){
  20. $tables[]['TABLE_NAME'] = $row[0];
  21. }
  22. }
  23. //Replace all tables Table prefix
  24. if($_GET['prefix']){
  25. $prefix = 'czzj';
  26. foreach($tables as $key => $val){
  27. $tableName = $val['TABLE_NAME'];
  28. $string = explode('_',$tableName);
  29. if($string[0] != $prefix){
  30. $string[0] = $prefix;
  31. $newTableName = implode('_', $string );
  32. mysql_query('rename table '.$tableName.' TO '.$newTableName);
  33. }
  34. }
  35. echo "Replacement successful! ";exit();
  36. }
  37. //Loop to obtain the notes and column messages of all tables
  38. foreach ($tables as $k=>$v) {
  39. $sql = 'SELECT * FROM ';
  40. $ sql .= 'INFORMATION_SCHEMA.TABLES ';
  41. $sql .= 'WHERE ';
  42. $sql .= "table_name = '{$v['TABLE_NAME']}' AND table_schema = '{$database}'";
  43. $ table_result = mysql_query($sql, $mysql_conn);
  44. while ($t = mysql_fetch_array($table_result) ) {
  45. $tables[$k]['TABLE_COMMENT'] = $t['TABLE_COMMENT'];
  46. }
  47. $ sql = 'SELECT * FROM ';
  48. $sql .= 'INFORMATION_SCHEMA.COLUMNS ';
  49. $sql .= 'WHERE ';
  50. $sql .= "table_name = '{$v['TABLE_NAME']}' AND table_schema = '{$database}'";
  51. $fields = array();
  52. $field_result = mysql_query($sql, $mysql_conn);
  53. while ($t = mysql_fetch_array($field_result) ) {
  54. $fields[] = $ t;
  55. }
  56. $tables[$k]['COLUMN'] = $fields;
  57. }
  58. mysql_close($mysql_conn);
  59. $html = '';
  60. //Loop all tables
  61. foreach ($tables as $k=>$v) {
  62. $html .= '

    ' . ($k + 1) . ', ' . $v['TABLE_COMMENT'] .' ('. $v['TABLE_NAME' ]. ')

    '."n";
  63. $html .= ' '." n";
  64. $html .= '
  65. '."n";
  66. $html .= '
  67. '."n";
  68. $html .= '
  69. '."n";
  70. $html .= '
  71. '. "n";
  72. $html .= '
  73. '."n";
  74. $html .= '
  75. '."n" ;
  76. $html .= '
  77. '."n";
  78. $html .= '
  79. '."n";
  80. foreach ($v['COLUMN' ] as $f) {
  81. if(!is_array($no_show_field[$v['TABLE_NAME']])){
  82. $no_show_field[$v['TABLE_NAME']] = array();
  83. }
  84. if(!in_array ($f['COLUMN_NAME'],$no_show_field[$v['TABLE_NAME']])){
  85. $html .= '
  86. '."n";
  87. $html .= '
  88. '."n";
  89. $html .= '
  90. '."n";
  91. $html .= '
  92. '." n";
  93. $html .= '
  94. '."n";
  95. $html .= '
  96. '."n";
  97. $html .= '
  98. '."n";
  99. $html .= '
  100. '."n";
  101. }
  102. }
  103. $html .= '
  104. '."n";
  105. $html .= '
  106. Field name'."n";
  107. $html .= '
  108. Data type Default value Allow non-null Auto-increment Remarks
    ' . $f['COLUMN_NAME'] . ' ' . $f['COLUMN_TYPE '] . ' ' . $f['COLUMN_DEFAULT'] . ' ' . $f['IS_NULLABLE'] . ' ' . ($f['EXTRA']=='auto_increment'?'is':' ') . ' ' . $f['COLUMN_COMMENT'] . '
    '."n";
  109. }
  110. ?>
Copy code
  1. Tengsu Technology Co., Ltd. database data dictionary generation code< /title></li> <li><meta name="generator" content="ThinkDb V1.0" /></li> <li><meta name="author" content="Nanchang Tengsu Technology Co., Ltd. http://www.tensent.cn " /></li> <li><meta name="copyright" content="2008-2014 Tensent Inc." /></li> <li><style></li> <li>body, td, th { font-family: "Microsoft Yahei"; font- size: 14px; }</li> <li>.warp{margin:auto; width:900px;}</li> <li>.warp h3{margin:0px; padding:0px; line-height:30px; margin-top:10px;}</li> <li>table { border-collapse : collapse; border: 1px solid #CCC; background: #efefef; }</li> <li>table th { text-align: left; font-weight: bold; height: 26px; line-height: 26px; font-size: 14px; text- align:center; border: 1px solid #CCC; padding:5px;}</li> <li>table td { height: 20px; font-size: 14px; border: 1px solid #CCC; background-color: #fff; padding:5px;}</li> <li> .c1 { width: 120px; }</li> <li>.c2 { width: 120px; }</li> <li>.c3 { width: 150px; }</li> <li>.c4 { width: 80px; text-align:center;}</li> <li>.c5 { width: 80px; text -align:center;}</li> <li>.c6 { width: 270px; }</li> <li></style></li> <li></head></li> <li><body></li> <li><div class="warp"></li> <li> <h1 style=" text-align:center;">Tengsu Technology Co., Ltd. database data dictionary generation code</h1></li> <li><?php echo $html; ?></li> <li></div></li> <li></body></li> <li>< ;/html></li> </ol></div> <em onclick="copycode($('code_NtV'));">Copy code</em> </div> </td></tr></table> <div id="comment_52508" class="cm"> </div> <div id="post_rate_div_52508"></div> <br><br></div><div class="nphpQianMsg"><div class="clear"></div></div><div class="nphpQianSheng"><span>Statement:</span><div>The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn</div></div></div><div class="nphpSytBox"><span>Previous article:<a class="dBlack" title="Screenshot extra characters, Chinese and English, to avoid intercepting Chinese garbled characters" href="https://m.php.cn/faq/314180.html">Screenshot extra characters, Chinese and English, to avoid intercepting Chinese garbled characters</a></span><span>Next article:<a class="dBlack" title="Screenshot extra characters, Chinese and English, to avoid intercepting Chinese garbled characters" href="https://m.php.cn/faq/314184.html">Screenshot extra characters, Chinese and English, to avoid intercepting Chinese garbled characters</a></span></div><div class="nphpSytBox2"><div class="nphpZbktTitle"><h2>Related articles</h2><em><a href="https://m.php.cn/article.html" class="bBlack"><i>See more</i><b></b></a></em><div class="clear"></div></div><ins class="adsbygoogle" style="display:block" data-ad-format="fluid" data-ad-layout-key="-6t+ed+2i-1n-4w" data-ad-client="ca-pub-5902227090019525" data-ad-slot="8966999616"></ins><script> (adsbygoogle = window.adsbygoogle || []).push({}); </script><ul class="nphpXgwzList"><li><b></b><a href="https://m.php.cn/faq/1.html" title="How to use cURL to implement Get and Post requests in PHP" class="aBlack">How to use cURL to implement Get and Post requests in PHP</a><div class="clear"></div></li><li><b></b><a href="https://m.php.cn/faq/1.html" title="How to use cURL to implement Get and Post requests in PHP" class="aBlack">How to use cURL to implement Get and Post requests in PHP</a><div class="clear"></div></li><li><b></b><a href="https://m.php.cn/faq/1.html" title="How to use cURL to implement Get and Post requests in PHP" class="aBlack">How to use cURL to implement Get and Post requests in PHP</a><div class="clear"></div></li><li><b></b><a href="https://m.php.cn/faq/1.html" title="How to use cURL to implement Get and Post requests in PHP" class="aBlack">How to use cURL to implement Get and Post requests in PHP</a><div class="clear"></div></li><li><b></b><a href="https://m.php.cn/faq/2.html" title="All expression symbols in regular expressions (summary)" class="aBlack">All expression symbols in regular expressions (summary)</a><div class="clear"></div></li></ul></div></div><ins class="adsbygoogle" style="display:block" data-ad-format="autorelaxed" data-ad-client="ca-pub-5902227090019525" data-ad-slot="5027754603"></ins><script> (adsbygoogle = window.adsbygoogle || []).push({}); </script><footer><div class="footer"><div class="footertop"><img src="/static/imghwm/logo.png" alt=""><p>Public welfare online PHP training,Help PHP learners grow quickly!</p></div><div class="footermid"><a href="https://m.php.cn/about/us.html">About us</a><a href="https://m.php.cn/about/disclaimer.html">Disclaimer</a><a href="https://m.php.cn/update/article_0_1.html">Sitemap</a></div><div class="footerbottom"><p> © php.cn All rights reserved </p></div></div></footer><script>isLogin = 0;</script><script type="text/javascript" src="/static/layui/layui.js"></script><script type="text/javascript" src="/static/js/global.js?4.9.47"></script></div><script src="https://vdse.bdstatic.com//search-video.v1.min.js"></script><link rel='stylesheet' id='_main-css' href='/static/css/viewer.min.css' type='text/css' media='all'/><script type='text/javascript' src='/static/js/viewer.min.js?1'></script><script type='text/javascript' src='/static/js/jquery-viewer.min.js'></script><script>jQuery.fn.wait = function (func, times, interval) { var _times = times || -1, //100次 _interval = interval || 20, //20毫秒每次 _self = this, _selector = this.selector, //选择器 _iIntervalID; //定时器id if( this.length ){ //如果已经获取到了,就直接执行函数 func && func.call(this); } else { _iIntervalID = setInterval(function() { if(!_times) { //是0就退出 clearInterval(_iIntervalID); } _times <= 0 || _times--; //如果是正数就 -- _self = $(_selector); //再次选择 if( _self.length ) { //判断是否取到 func && func.call(_self); clearInterval(_iIntervalID); } }, _interval); } return this; } $("table.syntaxhighlighter").wait(function() { $('table.syntaxhighlighter').append("<p class='cnblogs_code_footer'><span class='cnblogs_code_footer_icon'></span></p>"); }); $(document).on("click", ".cnblogs_code_footer",function(){ $(this).parents('table.syntaxhighlighter').css('display','inline-table');$(this).hide(); }); $('.nphpQianCont').viewer({navbar:true,title:false,toolbar:false,movable:false,viewed:function(){$('img').click(function(){$('.viewer-close').trigger('click');});}}); </script></body><!-- Matomo --><script> var _paq = window._paq = window._paq || []; /* tracker methods like "setCustomDimension" should be called before "trackPageView" */ _paq.push(['trackPageView']); _paq.push(['enableLinkTracking']); (function() { var u="https://tongji.php.cn/"; _paq.push(['setTrackerUrl', u+'matomo.php']); _paq.push(['setSiteId', '9']); var d=document, g=d.createElement('script'), s=d.getElementsByTagName('script')[0]; g.async=true; g.src=u+'matomo.js'; s.parentNode.insertBefore(g,s); })(); </script><!-- End Matomo Code --></html>