MySQL Help Service


/ Published in: PHP
Save to your folder(s)



Copy this code and paste it in your HTML
  1. <?php
  2.  
  3. class MySQLHelpService
  4. {
  5. private $mysqli;
  6.  
  7. public function __construct( $host, $username, $password )
  8. {
  9. //temporary for the bs warning signs on live
  10. // Report simple running errors
  11. error_reporting ( E_ERROR | E_USER_ERROR | E_PARSE );
  12.  
  13. $this->mysqli = new mysqli ( $host, $username, $password );
  14. /* check connection */
  15. {
  16. trigger_error ( 'Database connection failure: Username/Password was incorrect.', E_USER_ERROR );
  17. exit ();
  18. }
  19. else
  20. {
  21. return $this->mysqli;
  22. }
  23. }
  24.  
  25. public function getHelpTree()
  26. {
  27. $helpTree [] = array (
  28. 'label' => 'MySQL Help', 'children' => $this->_getAllHelp ()
  29. );
  30.  
  31. return json_encode ( $helpTree );
  32. }
  33.  
  34. public function _getAllHelp()
  35. {
  36. $sql = "SELECT
  37. help_category.name as category,
  38. help_topic.name as label,
  39. help_topic.description as description,
  40. help_topic.example as example
  41. FROM mysql.help_relation
  42. INNER JOIN mysql.help_topic
  43. ON help_relation.help_topic_id = help_topic.help_topic_id
  44. INNER JOIN mysql.help_category
  45. ON help_category.help_category_id = help_topic.help_category_id
  46. ORDER BY category ASC";
  47.  
  48. return $this->_queryToARRAY ( $sql );
  49. }
  50.  
  51. public function _getDataDefinition()
  52. {
  53. $sql = "SELECT
  54. help_keyword.name as keyword,
  55. help_category.name as category,
  56. help_topic.name as topic,
  57. help_topic.description as description,
  58. help_topic.example as example
  59. FROM mysql.help_relation
  60. INNER JOIN mysql.help_keyword
  61. ON help_relation.help_keyword_id = help_keyword.help_keyword_id
  62. INNER JOIN mysql.help_topic
  63. ON help_relation.help_topic_id = help_topic.help_topic_id
  64. INNER JOIN mysql.help_category
  65. ON help_category.help_category_id = help_topic.help_category_id
  66. WHERE help_category.name = 'Data Definition'
  67. ORDER BY topic ASC";
  68. return $this->_queryToArray ( $sql );
  69. }
  70.  
  71. public function _getDataManipulation()
  72. {
  73. $sql = "SELECT
  74. help_keyword.name as keyword,
  75. help_category.name as category,
  76. help_topic.name as topic,
  77. help_topic.description as description,
  78. help_topic.example as example
  79. FROM mysql.help_relation
  80. INNER JOIN mysql.help_keyword
  81. ON help_relation.help_keyword_id = help_keyword.help_keyword_id
  82. INNER JOIN mysql.help_topic
  83. ON help_relation.help_topic_id = help_topic.help_topic_id
  84. INNER JOIN mysql.help_category
  85. ON help_category.help_category_id = help_topic.help_category_id
  86. WHERE help_category.name = 'Data Manipulation'
  87. ORDER BY topic ASC";
  88. return $this->_queryToArray ( $sql );
  89. }
  90.  
  91. public function _getTableMaintenance()
  92. {
  93. $sql = "SELECT help_keyword.name, help_category.name, help_topic.name, help_topic.description, help_topic.example
  94. FROM mysql.help_relation
  95. INNER JOIN mysql.help_keyword
  96. ON help_relation.help_keyword_id = help_keyword.help_keyword_id
  97. INNER JOIN mysql.help_topic
  98. ON help_relation.help_topic_id = help_topic.help_topic_id
  99. INNER JOIN mysql.help_category
  100. ON help_category.help_category_id = help_topic.help_category_id
  101. WHERE help_category.name = 'Table Maintenance'";
  102. return $this->_queryToArray ( $sql );
  103. }
  104.  
  105. public function _getStoredRoutines()
  106. {
  107. $sql = "";
  108. return $this->_queryToArray ( $sql );
  109. }
  110.  
  111. public function _getAdministration()
  112. {
  113. $sql = "SELECT
  114. help_keyword.name as keyword,
  115. help_category.name as category,
  116. help_topic.name as topic,
  117. help_topic.description as description,
  118. help_topic.example as example
  119. FROM mysql.help_relation
  120. INNER JOIN mysql.help_keyword
  121. ON help_relation.help_keyword_id = help_keyword.help_keyword_id
  122. INNER JOIN mysql.help_topic
  123. ON help_relation.help_topic_id = help_topic.help_topic_id
  124. INNER JOIN mysql.help_category
  125. ON help_category.help_category_id = help_topic.help_category_id
  126. WHERE help_category.name = 'Administration'
  127. ORDER BY topic ASC";
  128. return $this->_queryToArray ( $sql );
  129. }
  130.  
  131. public function _getDataTypes()
  132. {
  133. $sql = "SELECT
  134. help_keyword.name as keyword,
  135. help_category.name as category,
  136. help_topic.name as topic,
  137. help_topic.description as description,
  138. help_topic.example as example
  139. FROM mysql.help_relation
  140. INNER JOIN mysql.help_keyword
  141. ON help_relation.help_keyword_id = help_keyword.help_keyword_id
  142. INNER JOIN mysql.help_topic
  143. ON help_relation.help_topic_id = help_topic.help_topic_id
  144. INNER JOIN mysql.help_category
  145. ON help_category.help_category_id = help_topic.help_category_id
  146. WHERE help_category.name = 'Data Types'
  147. ORDER BY topic ASC";
  148. return $this->_queryToArray ( $sql );
  149. }
  150.  
  151. public function _getAccountManagement()
  152. {
  153. $sql = "SELECT
  154. help_keyword.name as keyword,
  155. help_category.name as category,
  156. help_topic.name as topic,
  157. help_topic.description as description,
  158. help_topic.example as example
  159. FROM mysql.help_relation
  160. INNER JOIN mysql.help_keyword
  161. ON help_relation.help_keyword_id = help_keyword.help_keyword_id
  162. INNER JOIN mysql.help_topic
  163. ON help_relation.help_topic_id = help_topic.help_topic_id
  164. INNER JOIN mysql.help_category
  165. ON help_category.help_category_id = help_topic.help_category_id
  166. WHERE help_category.name = 'Account Management'
  167. ORDER BY topic ASC";
  168. return $this->_queryToArray ( $sql );
  169. }
  170.  
  171. public function _getStringFunctions()
  172. {
  173. $sql = "SELECT
  174. help_keyword.name as keyword,
  175. help_category.name as category,
  176. help_topic.name as topic,
  177. help_topic.description as description,
  178. help_topic.example as example
  179. FROM mysql.help_relation
  180. INNER JOIN mysql.help_keyword
  181. ON help_relation.help_keyword_id = help_keyword.help_keyword_id
  182. INNER JOIN mysql.help_topic
  183. ON help_relation.help_topic_id = help_topic.help_topic_id
  184. INNER JOIN mysql.help_category
  185. ON help_category.help_category_id = help_topic.help_category_id
  186. WHERE help_category.name = 'String Functions'
  187. ORDER BY topic ASC";
  188. return $this->_queryToArray ( $sql );
  189. }
  190.  
  191. public function _getControlFlow()
  192. {
  193. $sql = " SELECT help_keyword.name, help_category.name, help_topic.name, help_topic.description, help_topic.example
  194. FROM mysql.help_relation
  195. INNER JOIN mysql.help_keyword
  196. ON help_relation.help_keyword_id = help_keyword.help_keyword_id
  197. INNER JOIN mysql.help_topic
  198. ON help_relation.help_topic_id = help_topic.help_topic_id
  199. INNER JOIN mysql.help_category
  200. ON help_category.help_category_id = help_topic.help_category_id
  201. WHERE help_category.name = 'Control flow functions'";
  202. return $this->_queryToArray ( $sql );
  203. }
  204.  
  205. public function _gettTransactions()
  206. {
  207. $sql = "SELECT help_keyword.name, help_category.name, help_topic.name, help_topic.description, help_topic.example
  208. FROM mysql.help_relation
  209. INNER JOIN mysql.help_keyword
  210. ON help_relation.help_keyword_id = help_keyword.help_keyword_id
  211. INNER JOIN mysql.help_topic
  212. ON help_relation.help_topic_id = help_topic.help_topic_id
  213. INNER JOIN mysql.help_category
  214. ON help_category.help_category_id = help_topic.help_category_id
  215. WHERE help_category.name = 'Transactions'";
  216. return $this->_queryToArray ( $sql );
  217. }
  218.  
  219. public function _getFunctions()
  220. {
  221. $sql = "SELECT help_keyword.name, help_category.name, help_topic.name, help_topic.description, help_topic.example
  222. FROM mysql.help_relation
  223. INNER JOIN mysql.help_keyword
  224. ON help_relation.help_keyword_id = help_keyword.help_keyword_id
  225. INNER JOIN mysql.help_topic
  226. ON help_relation.help_topic_id = help_topic.help_topic_id
  227. INNER JOIN mysql.help_category
  228. ON help_category.help_category_id = help_topic.help_category_id
  229. WHERE help_category.name = 'Functions'";
  230. return $this->_queryToArray ( $sql );
  231. }
  232.  
  233. public function _getWKT()
  234. {
  235. $sql = "SELECT help_keyword.name, help_category.name, help_topic.name, help_topic.description, help_topic.example
  236. FROM mysql.help_relation
  237. INNER JOIN mysql.help_keyword
  238. ON help_relation.help_keyword_id = help_keyword.help_keyword_id
  239. INNER JOIN mysql.help_topic
  240. ON help_relation.help_topic_id = help_topic.help_topic_id
  241. INNER JOIN mysql.help_category
  242. ON help_category.help_category_id = help_topic.help_category_id
  243. WHERE help_category.name = 'WKB'";
  244. return $this->_queryToArray ( $sql );
  245. }
  246.  
  247. public function _getWKB()
  248. {
  249. $sql = "SELECT help_keyword.name, help_category.name, help_topic.name, help_topic.description, help_topic.example
  250. FROM mysql.help_relation
  251. INNER JOIN mysql.help_keyword
  252. ON help_relation.help_keyword_id = help_keyword.help_keyword_id
  253. INNER JOIN mysql.help_topic
  254. ON help_relation.help_topic_id = help_topic.help_topic_id
  255. INNER JOIN mysql.help_category
  256. ON help_category.help_category_id = help_topic.help_category_id
  257. WHERE help_category.name = 'WKB'";
  258. return $this->_queryToArray ( $sql );
  259. }
  260.  
  261. public function _getNumericFunctions()
  262. {
  263. $sql = "SELECT help_keyword.name, help_category.name, help_topic.name, help_topic.description, help_topic.example
  264. FROM mysql.help_relation
  265. INNER JOIN mysql.help_keyword
  266. ON help_relation.help_keyword_id = help_keyword.help_keyword_id
  267. INNER JOIN mysql.help_topic
  268. ON help_relation.help_topic_id = help_topic.help_topic_id
  269. INNER JOIN mysql.help_category
  270. ON help_category.help_category_id = help_topic.help_category_id
  271. WHERE help_category.name = 'Numeric Functions'";
  272. return $this->_queryToArray ( $sql );
  273. }
  274.  
  275. public function _getLanguageStructure()
  276. {
  277. $sql = "SELECT help_keyword.name, help_category.name, help_topic.name, help_topic.description, help_topic.example
  278. FROM mysql.help_relation
  279. INNER JOIN mysql.help_keyword
  280. ON help_relation.help_keyword_id = help_keyword.help_keyword_id
  281. INNER JOIN mysql.help_topic
  282. ON help_relation.help_topic_id = help_topic.help_topic_id
  283. INNER JOIN mysql.help_category
  284. ON help_category.help_category_id = help_topic.help_category_id
  285. WHERE help_category.name = 'Language Structure'";
  286. return $this->_queryToArray ( $sql );
  287. }
  288.  
  289. public function _getComparison()
  290. {
  291. $sql = "SELECT help_keyword.name, help_category.name, help_topic.name, help_topic.description, help_topic.example
  292. FROM mysql.help_relation
  293. INNER JOIN mysql.help_keyword
  294. ON help_relation.help_keyword_id = help_keyword.help_keyword_id
  295. INNER JOIN mysql.help_topic
  296. ON help_relation.help_topic_id = help_topic.help_topic_id
  297. INNER JOIN mysql.help_category
  298. ON help_category.help_category_id = help_topic.help_category_id
  299. WHERE help_category.name = 'Comparison operators'";
  300. return $this->_queryToArray ( $sql );
  301. }
  302.  
  303. public function _getDateTime()
  304. {
  305. $sql = "SELECT help_keyword.name, help_category.name, help_topic.name, help_topic.description, help_topic.example
  306. FROM mysql.help_relation
  307. INNER JOIN mysql.help_keyword
  308. ON help_relation.help_keyword_id = help_keyword.help_keyword_id
  309. INNER JOIN mysql.help_topic
  310. ON help_relation.help_topic_id = help_topic.help_topic_id
  311. INNER JOIN mysql.help_category
  312. ON help_category.help_category_id = help_topic.help_category_id
  313. WHERE help_category.name = 'Date and Time Functions'";
  314. return $this->_queryToArray ( $sql );
  315. }
  316.  
  317. public function _getLogicalOpperators()
  318. {
  319. $sql = "SELECT help_keyword.name, help_category.name, help_topic.name, help_topic.description, help_topic.example
  320. FROM mysql.help_relation
  321. INNER JOIN mysql.help_keyword
  322. ON help_relation.help_keyword_id = help_keyword.help_keyword_id
  323. INNER JOIN mysql.help_topic
  324. ON help_relation.help_topic_id = help_topic.help_topic_id
  325. INNER JOIN mysql.help_category
  326. ON help_category.help_category_id = help_topic.help_category_id
  327. WHERE help_category.name = 'Logical operators'";
  328. return $this->_queryToArray ( $sql );
  329. }
  330.  
  331. public function _getEncryptionFunctions()
  332. {
  333. $sql = "SELECT help_keyword.name, help_category.name, help_topic.name, help_topic.description, help_topic.example
  334. FROM mysql.help_relation
  335. INNER JOIN mysql.help_keyword
  336. ON help_relation.help_keyword_id = help_keyword.help_keyword_id
  337. INNER JOIN mysql.help_topic
  338. ON help_relation.help_topic_id = help_topic.help_topic_id
  339. INNER JOIN mysql.help_category
  340. ON help_category.help_category_id = help_topic.help_category_id
  341. WHERE help_category.name = 'Encryption Functions'";
  342. return $this->_queryToArray ( $sql );
  343. }
  344.  
  345. public function _getGeographicFeatures()
  346. {
  347. $sql = "SELECT help_keyword.name, help_category.name, help_topic.name, help_topic.description, help_topic.example
  348. FROM mysql.help_relation
  349. INNER JOIN mysql.help_keyword
  350. ON help_relation.help_keyword_id = help_keyword.help_keyword_id
  351. INNER JOIN mysql.help_topic
  352. ON help_relation.help_topic_id = help_topic.help_topic_id
  353. INNER JOIN mysql.help_category
  354. ON help_category.help_category_id = help_topic.help_category_id
  355. WHERE help_category.name = 'Geographic Features'";
  356. return $this->_queryToArray ( $sql );
  357. }
  358.  
  359. public function _getTriggers()
  360. {
  361. $sql - "SELECT help_keyword.name, help_category.name, help_topic.name, help_topic.description, help_topic.example
  362. FROM mysql.help_relation
  363. INNER JOIN mysql.help_keyword
  364. ON help_relation.help_keyword_id = help_keyword.help_keyword_id
  365. INNER JOIN mysql.help_topic
  366. ON help_relation.help_topic_id = help_topic.help_topic_id
  367. INNER JOIN mysql.help_category
  368. ON help_category.help_category_id = help_topic.help_category_id
  369. WHERE help_category.name = 'Triggers'";
  370. return $this->_queryToArray ( $sql );
  371. }
  372.  
  373. /* ********************************************************************
  374. * ********************************************************************
  375. *
  376. * 8. RESULT HANDLERS
  377. *
  378. * ********************************************************************
  379. * ********************************************************************/
  380.  
  381. /**
  382. * I execute a query and return the results as json.
  383. *
  384. * @param [string] $sql the query to be executed
  385. * @return [json] the result in json
  386. */
  387. private function _queryToJSON( $sql )
  388. {
  389. $result = mysqli_query ( $this->mysqli, $sql );
  390.  
  391. while ( $row = mysqli_fetch_assoc ( $result ) )
  392. {
  393. $array [] = $row;
  394. }
  395. return json_encode ( $array );
  396. }
  397.  
  398. /**
  399. * I execute a query and return the result as an array.
  400. *
  401. * @param [string] $sql the query to be executed
  402. * @return [array] the result array
  403. */
  404. private function _queryToARRAY( $sql )
  405. {
  406. $query = mysqli_query ( $this->mysqli, $sql );
  407. $array = array ();
  408.  
  409. while ( $row = mysqli_fetch_assoc ( $query ) )
  410. {
  411. $array [] = $row;
  412. }
  413.  
  414. return $array;
  415. }
  416.  
  417. }
  418.  
  419. //testing
  420. //$help = new MySQLHelpService( 'localhost', 'root', 'fred' );
  421. //print_r( $help->getHelpTree() );
  422.  
  423.  
  424. ?>

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.