Friday, 7 July 2017

php mysql mysqli quries class

Below class file handle mysql and mysqli connection & quries. you can select all records as well as can place where clause also saving records , removing records , executing direct query with Mysql OR MySqli as defined in config.php file.
1. Your config.php file will be as below , you can change it and placed values accordingly as your mysql database. resource_type Mysql OR MySqli
  1. $dbconfig['db_server'] = 'localhost';
  2. $dbconfig['db_port'] = ':3306';
  3. $dbconfig['db_username'] = 'root';
  4. $dbconfig['db_password'] = '';
  5. $dbconfig['db_name'] = 'test';
  6. $resource_type='MySqli';
2. Below is MysqlDatabaseMysqli.php class which has functions , you can execute Queries with MySql or MySqli.
  1. require_once 'config.php';
  2. Class MysqlDatabaseMysqli {
  3. protected $connection = NULL;
  4. protected $connection_string = NULL;
  5. protected $associate_type = NULL;
  6. function __construct() {
  7. global $resource_type;
  8. $this->resource_type_string = strtolower($resource_type);
  9. switch ($resource_type) {
  10. case 'MySql':
  11. $this->associate_type = MYSQL_ASSOC;
  12. $this->msl_connect();
  13. break;
  14. case 'MySqli':
  15. $this->associate_type = MYSQLI_ASSOC;
  16. $this->msli_connect();
  17. break;
  18. default:
  19. echo "No resource type define";
  20. exit;
  21. break;
  22. }
  23. }
  24. /*
  25. * msl->mysql
  26. */
  27. function msl_connect() {
  28. global $dbconfig;
  29. try {
  30. if ($link_identifier = @mysql_connect($dbconfig['db_server'],
  31. $dbconfig['db_username'], $dbconfig['db_password'])) {
  32. $select_db = @mysql_select_db($dbconfig['db_name'], $link_identifier);
  33. if (!$select_db) {
  34. throw new Exception(mysql_errno() . ' ' . mysql_error());
  35. }
  36. $this->connection = $link_identifier;
  37. } else {
  38. throw new Exception(mysql_errno() . ' ' . mysql_error());
  39. }
  40. } catch (Exception $e) {
  41. echo $e->getMessage();
  42. }
  43. }
  44. /*
  45. * msli->mysqli
  46. */
  47. function msli_connect() {
  48. global $dbconfig;
  49. try {
  50. if ($link_identifier = @mysqli_connect($dbconfig['db_server']
  51. , $dbconfig['db_username'], $dbconfig['db_password'])) {
  52. $select_db = @mysqli_select_db($link_identifier, $dbconfig['db_name']);
  53. if (!$select_db) {
  54. throw new Exception(mysqli_connect_errno() . ' Unable to select db');
  55. }
  56. $this->connection = $link_identifier;
  57. } else {
  58. throw new Exception(mysqli_connect_errno() . ' Unable to connect');
  59. }
  60. } catch (Exception $e) {
  61. echo $e->getMessage();
  62. }
  63. }
  64. /*
  65. * Created By: Boy108zon
  66. * @table_name: string
  67. * @where: Array
  68. */
  69. function get_records($table_name, $where=NULL) {
  70. $connect = $this->resource_type_string . '_query';
  71. $num_rows = $this->resource_type_string . '_num_rows';
  72. $connect_close = $this->resource_type_string . '_close';
  73. /* if where included */
  74. $qwhere = '';
  75. if (is_array($where)) {
  76. $counter = 0;
  77. foreach ($where as $key => $value) {
  78. if ($counter > 0) {
  79. $qwhere .= ' AND ';
  80. }
  81. $qwhere .= "$key = '$value'";
  82. $counter++;
  83. }
  84. $qwhere = 'WHERE ' . $qwhere;
  85. }
  86. $query = "SELECT * from $table_name $qwhere";
  87. if ($this->resource_type_string == 'mysqli') {
  88. $result_resource = $connect($this->connection, $query);
  89. } else {
  90. $result_resource = $connect($query, $this->connection);
  91. }
  92. $noOfrows = $num_rows($result_resource);
  93. if ($noOfrows > 0) {
  94. return $this->db_to_array($result_resource);
  95. } else {
  96. return NULL;
  97. }
  98. }
  99. /*
  100. * Created By: Boy108zon
  101. * @table_name: string
  102. * @where: Array
  103. */
  104. function remove_records($table_name, $where=NULL) {
  105. $connect = $this->resource_type_string . '_query';
  106. $affected_rows = $this->resource_type_string . '_affected_rows';
  107. $connect_close = $this->resource_type_string . '_close';
  108. /* if where included */
  109. $qwhere = '';
  110. if (is_array($where)) {
  111. $counter = 0;
  112. foreach ($where as $key => $value) {
  113. if ($counter > 0) {
  114. $qwhere .= ' AND ';
  115. }
  116. $qwhere .= "$key = '$value'";
  117. $counter++;
  118. }
  119. $qwhere = 'WHERE ' . $qwhere;
  120. }
  121. $query = "DELETE from $table_name $qwhere";
  122. if ($this->resource_type_string == 'mysqli') {
  123. $result_resource = $connect($this->connection, $query);
  124. } else {
  125. $result_resource = $connect($query, $this->connection);
  126. }
  127. $idds = $affected_rows($this->connection);
  128. //close connection
  129. @$connect_close($this->connection);
  130. if ($idds > 0) {
  131. return $idds;
  132. } else {
  133. return NULL;
  134. }
  135. }
  136. /*
  137. * Created By: Boy108zon
  138. * @result->mysql OR mysqli
  139. */
  140. function db_to_array($result) {
  141. $connection_string = $this->resource_type_string . '_fetch_array';
  142. $rows = array();
  143. while ($row = $connection_string($result, $this->associate_type)) {
  144. $rows[] = $row;
  145. }
  146. //close connection
  147. @$connect_close($this->connection);
  148. return $rows;
  149. }
  150. /*
  151. * Created By: Boy108zon
  152. * @table_name: String
  153. * @post_array:Array
  154. * @where:Array
  155. * @mode:'save' OR edit
  156. * Based on mode return type affected rows and latest insert id
  157. */
  158. function save_records($table_name, $post_array, $where=NULL, $mode='save') {
  159. $connect = $this->resource_type_string . '_query';
  160. $insert_id = $this->resource_type_string . '_insert_id';
  161. $affected_rows = $this->resource_type_string . '_affected_rows';
  162. $connect_close = $this->resource_type_string . '_close';
  163. $count = 0;
  164. $fields = '';
  165. foreach ($post_array as $col => $val) {
  166. if ($count > 0) {
  167. $fields .= ', ';
  168. }
  169. $fields .= "$col = '$val'";
  170. $count++;
  171. }
  172. $qwhere = '';
  173. if (is_array($where)) {
  174. $counter = 0;
  175. foreach ($where as $key => $value) {
  176. if ($counter > 0) {
  177. $qwhere .= ' AND ';
  178. }
  179. $qwhere .= "$key = '$value'";
  180. $counter++;
  181. }
  182. $qwhere = 'WHERE ' . $qwhere;
  183. }
  184. if ($mode == 'edit') {
  185. $query = "UPDATE " . $table_name . " SET $fields $qwhere;";
  186. } else {
  187. $query = "INSERT INTO " . $table_name . " SET $fields;";
  188. }
  189. if ($this->resource_type_string == 'mysqli') {
  190. $result_resource = $connect($this->connection, $query);
  191. } else {
  192. $result_resource = $connect($query, $this->connection);
  193. }
  194. if ($mode == 'edit') {
  195. $idds = $affected_rows($this->connection);
  196. } else {
  197. $idds = $insert_id($this->connection);
  198. }
  199. //close connection
  200. @$connect_close($this->connection);
  201. if ($idds > 0) {
  202. return $idds;
  203. } else {
  204. return NULL;
  205. }
  206. }
  207. /*
  208. * Created By: Boy108zon
  209. * @query: full query including inner , where
  210. * if required we can also use it.
  211. */
  212. function get_direct_query_records($query) {
  213. $connect = $this->resource_type_string . '_query';
  214. $num_rows = $this->resource_type_string . '_num_rows';
  215. $connect_close = $this->resource_type_string . '_close';
  216. if ($this->resource_type_string == 'mysqli') {
  217. $result_resource = $connect($this->connection, $query);
  218. } else {
  219. $result_resource = $connect($query, $this->connection);
  220. }
  221. $noOfrows = $num_rows($result_resource);
  222. //close connection
  223. @$connect_close($this->connection);
  224. if ($noOfrows > 0) {
  225. return $this->db_to_array($result_resource);
  226. } else {
  227. return NULL;
  228. }
  229. }
  230. }
3. You can use it like below
  1. require_once 'MysqlDatabaseMysqli.php';
  2. $dbfun=new MysqlDatabaseMysqli();
  3.  
  4. $result=$dbfun->get_records('employee',array('name'=>'amit'));
  5. //For Retrive records from employee table.
  6. $result=$dbfun->get_records('your_table_name');
  7. //edit
  8. $result=$dbfun->save_records('your_table_name',$post_array,$where,$mode='Edit');
  9. //save
  10. $result=$dbfun->save_records('your_table_name',$post_array);
  11. //remove
  12. $result=$dbfun->remove_records('your_table_name',array('id'=>6));
  13. // direct query
  14. $result=$dbfun->get_direct_query_records($query='select * from employee');
4. You can download class from below
  1. https://github.com/boy108zon/SingelMysqlMysqli