• We just launched and are currently in beta. Join us as we build and grow the community.

Creating an SQLite Custom CRUD Class in PHP Tutorial

boundnik

Social Presence Architect
B Rep
0
0
0
Rep
0
B Vouches
0
0
0
Vouches
0
Posts
161
Likes
134
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 300 XP
In this tutorial, you will learn how to create a Custom CRUD Class of SQLite Queries using PHP Language. This tutorial aims to provide IT/CS students and new programmers with a reference for enhancing their knowledge and programming capabilities using PHP and SQLite3 Database. Here, snippets for achieving the objectives of this tutorial are provided. Also, a working sample source code zip file that demonstrates the usage of the Custom CRUD Class is provided and free to download.

What is the purpose of Custom SQLite CRUD Class in PHP?

The Custom SQLite Class in PHP is a simple PHP object that developers can use for handling database queries and execution in a PHP Project. Since the object is for CRUD (Create, Read, Update, and delete) operations of an application or program, the object contains the methods for simplifying the queries for the said operations. It can help developers to write codes or develop their projects easier and faster.

How to Create an SQLite Custom CRUD Class in PHP?

Well, PHP version 5 or later version comes with SQLite3 class which is an object that interfaces SQLite version 3 Database. You can enable this class or object by enabling the sqlite3 extension on your php.ini

file. See the following image for enabling or uncommenting the said extension:

Next, before we create the custom CRUD class, we must create first the database connection object for the application and the SQLite3. The following snippet is an example of creating the SQLite3 Database Connection in PHP. The database connection object provided below contains also the method that creates the database table.

  1. <?php
  2. class

    Database extends

    SQLite3 {
  3. public

    function

    __construct(

    $db_file_path

    =

    ""

    )

    {
  4. if

    (

    empty

    (

    $db_file_path

    )

    )

    {
  5. throw

    new

    ErrorException(

    "SQLite DB file is not set."

    )

    ;
  6. exit

    ;
  7. }
  8. $this

    ->

    open

    (

    $db_file_path

    )

    ;
  9. if

    (

    $this

    ->

    lastErrorCode

    (

    )

    >

    0

    )

    {
  10. throw

    new

    ErrorException(

    "Database Connection failed."

    )

    ;
  11. exit

    ;
  12. }
  13. $this

    ->

    create_post_table

    (

    )

    ;
  14. }

  15. public

    function

    create_post_table(

    )

    {
  16. /**
  17. * Creating a sample post table
  18. */
  19. $post_tbl

    =

    "CREATE TABLE IF NOT EXISTS `posts`
  20. (
  21. `id` INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
  22. `title` text NOT NULL,
  23. `meta_description` text NOT NULL,
  24. `content` text NULL DEFAULT NULL,
  25. `created_at` datetime DEFAULT CURRENT_TIMESTAMP,
  26. `updated_at` datetime NULL DEFAULT NULL
  27. )
  28. "

    ;
  29. $this

    ->

    exec

    (

    $post_tbl

    )

    ;
  30. if

    (

    $this

    ->

    lastErrorCode

    (

    )

    )

    {
  31. throw

    new

    ErrorException(

    $this

    ->

    lastErrorMsg

    (

    )

    ,

    $this

    ->

    lastErrorCode

    (

    )

    )

    ;
  32. }
  33. /**
  34. * POST update datetime trigger
  35. */
  36. $post_dt_trigger

    =

    "CREATE TRIGGER IF NOT EXISTS [UpdateAT]
  37. AFTER
  38. UPDATE
  39. ON `posts`
  40. FOR EACH ROW
  41. BEGIN
  42. UPDATE `posts` set `updated_at` = CURRENT_TIMESTAMP where `id`=OLD.`id`;
  43. END
  44. "

    ;
  45. $this

    ->

    exec

    (

    $post_dt_trigger

    )

    ;
  46. if

    (

    $this

    ->

    lastErrorCode

    (

    )

    )

    {
  47. throw

    new

    ErrorException(

    $this

    ->

    lastErrorMsg

    (

    )

    ,

    $this

    ->

    lastErrorCode

    (

    )

    )

    ;
  48. }
  49. }

  50. public

    function

    __destruct(

    )

    {
  51. /**
  52. * destruct the sample class object
  53. */
  54. $this

    ->

    close

    (

    )

    ;
  55. }
  56. }
  57. if

    (

    !

    is_dir

    (

    "./database"

    )

    )
  58. mkdir

    (

    "./database"

    )

    ;
  59. // $database = new Database();


Next, we'll now create the custom CRUD class or object. The object must derive the SQLite3 class or the Custom SQLite3 Database class so we can access the properties of the SQLite for executing the database queries. Check out the snippet below.

  1. <?php
  2. class

    DBCRUD extends

    DATABASE{
  3. public

    function

    insert(

    $tbl

    =

    ""

    ,

    $data

    =

    [

    ]

    )

    {
  4. if

    (

    empty

    (

    $tbl

    )

    )

    {
  5. throw

    new

    ErrorException(

    "Insertion Table Name must be specified."

    )

    ;
  6. }
  7. if

    (

    empty

    (

    $data

    )

    )

    {
  8. throw

    new

    ErrorException(

    "Insertion required column and data must be specified."

    )

    ;
  9. }

  10. $insert_columns

    =

    [

    ]

    ;
  11. $insert_values

    =

    [

    ]

    ;
  12. foreach

    (

    $data

    as

    $k

    =>

    $v

    )

    {
  13. if

    (

    !

    is_numeric

    (

    $v

    )

    &&

    !

    is_array

    (

    $data

    [

    $k

    ]

    )

    &&

    !

    empty

    (

    $v

    )

    )

    {
  14. $v

    =

    $this

    ->

    escapeString

    (

    addslashes

    (

    $v

    )

    )

    ;
  15. }
  16. $insert_columns

    [

    ]

    =

    $k

    ;
  17. $insert_values

    [

    ]

    =

    $v

    ;
  18. }

  19. $tbl_columns

    =

    implode

    (

    "`, `"

    ,

    $insert_columns

    )

    ;
  20. $tbl_values

    =

    implode

    (

    "', '"

    ,

    $insert_values

    )

    ;
  21. $insert_sql

    =

    "INSERT INTO `{$tbl}

    `
  22. (`{$tbl_columns}

    `)
  23. VALUES
  24. ('{$tbl_values}

    ')
  25. "

    ;
  26. try{
  27. $this

    ->

    exec

    (

    $insert_sql

    )

    ;
  28. if

    (

    !

    $this

    ->

    lastErrorCode

    (

    )

    )

    {
  29. $resp

    [

    'status'

    ]

    =

    "success"

    ;
  30. $last_id

    =

    $this

    ->

    lastInsertRowid

    (

    )

    ;
  31. $resp

    [

    'id'

    ]

    =

    $last_id

    ;
  32. }

    else

    {
  33. throw

    new

    ErrorException(

    $this

    ->

    lastErrorMsg

    (

    )

    ,

    $this

    ->

    lastErrorCode

    (

    )

    )

    ;
  34. }
  35. }

    catch(

    Exception $e

    )

    {
  36. $resp

    [

    'status'

    ]

    =

    "failed"

    ;
  37. $resp

    [

    'error'

    ]

    =

    $e

    ->

    getMessage

    (

    )

    ;

  38. }
  39. return

    $resp

    ;
  40. }
  41. public

    function

    update(

    $tbl

    =

    ""

    ,

    $data

    =

    [

    ]

    ,

    $where

    =

    [

    ]

    )

    {
  42. if

    (

    empty

    (

    $tbl

    )

    )

    {
  43. throw

    new

    ErrorException(

    "Update Table Name must be specified."

    )

    ;
  44. }
  45. if

    (

    empty

    (

    $data

    )

    )

    {
  46. throw

    new

    ErrorException(

    "Update required column and data must be specified."

    )

    ;
  47. }

  48. if

    (

    empty

    (

    $where

    )

    )

    {
  49. throw

    new

    ErrorException(

    "Update Where Array must be specified."

    )

    ;
  50. }
  51. $update_data

    =

    ""

    ;
  52. foreach

    (

    $data

    as

    $k

    =>

    $v

    )

    {
  53. if

    (

    !

    is_numeric

    (

    $v

    )

    &&

    !

    is_array

    (

    $data

    [

    $k

    ]

    )

    &&

    !

    empty

    (

    $v

    )

    )

    {
  54. $v

    =

    $this

    ->

    escapeString

    (

    addslashes

    (

    $v

    )

    )

    ;
  55. }
  56. if

    (

    !

    empty

    (

    $update_data

    )

    )

    $update_data

    .=

    ", "

    ;
  57. $update_data

    .=

    "`{$k}

    `='{$v}

    '"

    ;
  58. }
  59. $where_data

    =

    ""

    ;
  60. foreach

    (

    $where

    as

    $k

    =>

    $v

    )

    {
  61. if

    (

    !

    is_numeric

    (

    $v

    )

    &&

    !

    is_array

    (

    $where

    [

    $k

    ]

    )

    &&

    !

    empty

    (

    $v

    )

    )

    {
  62. $v

    =

    $this

    ->

    escapeString

    (

    addslashes

    (

    $v

    )

    )

    ;
  63. }
  64. $type

    =

    "and"

    ;
  65. $compare

    =

    "="

    ;
  66. $value

    =

    $v

    ;
  67. if

    (

    is_array

    (

    $v

    )

    )

    {
  68. if

    (

    isset

    (

    $v

    [

    'value'

    ]

    )

    )
  69. $value

    =

    $v

    [

    'value'

    ]

    ;
  70. if

    (

    isset

    (

    $v

    [

    'type'

    ]

    )

    )
  71. $type

    =

    $v

    [

    'type'

    ]

    ;
  72. if

    (

    isset

    (

    $v

    [

    'compare'

    ]

    )

    )
  73. $compare

    =

    $v

    [

    'compare'

    ]

    ;
  74. }
  75. if

    (

    !

    empty

    (

    $where_data

    )

    )

    $where_data

    .=

    " {$type}

    "

    ;
  76. $where_data

    .=

    "`{$k}

    ` {$compare}

    "

    .

    (

    !

    empty

    (

    $value

    )

    ? "'{$value}

    '"

    :

    ""

    )

    ;
  77. }

  78. $update_sql

    =

    "UPDATE `{$tbl}

    ` set {$update_data}

    WHERE {$where_data}

    "

    ;
  79. try{
  80. $this

    ->

    exec

    (

    $update_sql

    )

    ;
  81. if

    (

    !

    $this

    ->

    lastErrorCode

    (

    )

    )

    {
  82. $resp

    [

    'status'

    ]

    =

    "success"

    ;
  83. }

    else

    {
  84. throw

    new

    ErrorException(

    $this

    ->

    lastErrorMsg

    (

    )

    ,

    $this

    ->

    lastErrorCode

    (

    )

    )

    ;
  85. }
  86. }

    catch(

    Exception $e

    )

    {
  87. $resp

    [

    'status'

    ]

    =

    "failed"

    ;
  88. $resp

    [

    'error'

    ]

    =

    $e

    ->

    getMessage

    (

    )

    ;

  89. }
  90. return

    $resp

    ;
  91. }
  92. public

    function

    delete(

    $tbl

    =

    ""

    ,

    $where

    =

    [

    ]

    )

    {
  93. if

    (

    empty

    (

    $tbl

    )

    )

    {
  94. throw

    new

    ErrorException(

    "Table Name for deleting must be specified."

    )

    ;
  95. }
  96. if

    (

    empty

    (

    $where

    )

    )

    {
  97. throw

    new

    ErrorException(

    "Where Array for deleting must be specified."

    )

    ;
  98. }
  99. $where_data

    =

    ""

    ;
  100. foreach

    (

    $where

    as

    $k

    =>

    $v

    )

    {
  101. if

    (

    !

    is_numeric

    (

    $v

    )

    &&

    !

    is_array

    (

    $where

    [

    $k

    ]

    )

    &&

    !

    empty

    (

    $v

    )

    )

    {
  102. $v

    =

    $this

    ->

    escapeString

    (

    addslashes

    (

    $v

    )

    )

    ;
  103. }
  104. $type

    =

    "and"

    ;
  105. $compare

    =

    "="

    ;
  106. $value

    =

    $v

    ;
  107. if

    (

    is_array

    (

    $v

    )

    )

    {
  108. if

    (

    isset

    (

    $v

    [

    'value'

    ]

    )

    )
  109. $value

    =

    $v

    [

    'value'

    ]

    ;
  110. if

    (

    isset

    (

    $v

    [

    'type'

    ]

    )

    )
  111. $type

    =

    $v

    [

    'type'

    ]

    ;
  112. if

    (

    isset

    (

    $v

    [

    'compare'

    ]

    )

    )
  113. $compare

    =

    $v

    [

    'compare'

    ]

    ;
  114. }
  115. if

    (

    !

    empty

    (

    $where_data

    )

    )

    $where_data

    .=

    " {$type}

    "

    ;
  116. $where_data

    .=

    "`{$k}

    ` {$compare}

    "

    .

    (

    !

    empty

    (

    $value

    )

    ? "'{$value}

    '"

    :

    ""

    )

    ;
  117. }
  118. $delete_sql

    =

    "DELETE FROM `{$tbl}

    ` where {$where_data}

    "

    ;
  119. try{
  120. $this

    ->

    exec

    (

    $delete_sql

    )

    ;
  121. if

    (

    !

    $this

    ->

    lastErrorCode

    (

    )

    )

    {
  122. $resp

    [

    'status'

    ]

    =

    "success"

    ;
  123. }

    else

    {
  124. throw

    new

    ErrorException(

    $this

    ->

    lastErrorMsg

    (

    )

    ,

    $this

    ->

    lastErrorCode

    (

    )

    )

    ;
  125. }
  126. }

    catch(

    Exception $e

    )

    {
  127. $resp

    [

    'status'

    ]

    =

    "failed"

    ;
  128. $resp

    [

    'error'

    ]

    =

    $e

    ->

    getMessage

    (

    )

    ;

  129. }
  130. return

    $resp

    ;
  131. }
  132. public

    function

    getSingle(

    $tbl

    ,

    $where

    =

    [

    ]

    ,

    $select

    =

    [

    ]

    )

    {
  133. if

    (

    empty

    (

    $tbl

    )

    )

    {
  134. throw

    new

    ErrorException(

    "Table Name for fetching data must be specified."

    )

    ;
  135. }
  136. if

    (

    empty

    (

    $where

    )

    )

    {
  137. throw

    new

    ErrorException(

    "Where Array for fetching data must be specified."

    )

    ;
  138. }
  139. $where_data

    =

    ""

    ;
  140. foreach

    (

    $where

    as

    $k

    =>

    $v

    )

    {
  141. if

    (

    !

    is_numeric

    (

    $v

    )

    &&

    !

    is_array

    (

    $where

    [

    $k

    ]

    )

    &&

    !

    empty

    (

    $v

    )

    )

    {
  142. $v

    =

    $this

    ->

    escapeString

    (

    addslashes

    (

    $v

    )

    )

    ;
  143. }
  144. $type

    =

    "and"

    ;
  145. $compare

    =

    "="

    ;
  146. $value

    =

    $v

    ;
  147. if

    (

    is_array

    (

    $v

    )

    )

    {
  148. if

    (

    isset

    (

    $v

    [

    'value'

    ]

    )

    )
  149. $value

    =

    $v

    [

    'value'

    ]

    ;
  150. if

    (

    isset

    (

    $v

    [

    'type'

    ]

    )

    )
  151. $type

    =

    $v

    [

    'type'

    ]

    ;
  152. if

    (

    isset

    (

    $v

    [

    'compare'

    ]

    )

    )
  153. $compare

    =

    $v

    [

    'compare'

    ]

    ;
  154. }
  155. if

    (

    !

    empty

    (

    $where_data

    )

    )

    $where_data

    .=

    " {$type}

    "

    ;
  156. $where_data

    .=

    "`{$k}

    ` {$compare}

    "

    .

    (

    !

    empty

    (

    $value

    )

    ? "'{$value}

    '"

    :

    ""

    )

    ;
  157. }
  158. $select_column

    =

    ""

    ;
  159. if

    (

    !

    empty

    (

    $select

    )

    )

    {
  160. foreach

    (

    $select

    as

    $k

    =>

    $v

    )

    {
  161. if

    (

    !

    is_numeric

    (

    $v

    )

    &&

    !

    is_array

    (

    $select

    [

    $k

    ]

    )

    &&

    !

    empty

    (

    $v

    )

    )

    {
  162. $v

    =

    $this

    ->

    escapeString

    (

    addslashes

    (

    $v

    )

    )

    ;
  163. }
  164. if

    (

    is_array

    (

    $v

    )

    &&

    is_numeric

    (

    $v

    )

    &&

    empty

    (

    $v

    )

    )
  165. continue

    ;
  166. if

    (

    !

    empty

    (

    $select_column

    )

    )

    $select_column

    .=

    ", "

    ;
  167. $select_column

    .=

    "`{$v}

    `"

    ;
  168. }
  169. }

    else

    {
  170. $select_column

    =

    "*"

    ;
  171. }
  172. $selectSingle_sql

    =

    "SELECT {$select_column}

    FROM `{$tbl}

    ` WHERE {$where_data}

    "

    ;
  173. // return $selectSingle_sql;
  174. $query

    =

    $this

    ->

    query

    (

    $selectSingle_sql

    )

    ;
  175. $data

    =

    $query

    ->

    fetchArray

    (

    SQLITE3_ASSOC)

    ;
  176. return

    !

    empty

    (

    $data

    )

    ? $data

    :

    [

    ]

    ;
  177. }
  178. public

    function

    getResults(

    $tbl

    ,

    $select

    =

    [

    ]

    ,

    $where

    =

    [

    ]

    ,

    $additional_qry

    =

    ""

    )

    {
  179. if

    (

    empty

    (

    $tbl

    )

    )

    {
  180. throw

    new

    ErrorException(

    "Table Name for fetching data must be specified."

    )

    ;
  181. }
  182. $where_data

    =

    ""

    ;
  183. foreach

    (

    $where

    as

    $k

    =>

    $v

    )

    {
  184. if

    (

    !

    is_numeric

    (

    $v

    )

    &&

    !

    is_array

    (

    $where

    [

    $k

    ]

    )

    &&

    !

    empty

    (

    $v

    )

    )

    {
  185. $v

    =

    $this

    ->

    escapeString

    (

    addslashes

    (

    $v

    )

    )

    ;
  186. }
  187. $type

    =

    "and"

    ;
  188. $compare

    =

    "="

    ;
  189. $value

    =

    $v

    ;
  190. if

    (

    is_array

    (

    $v

    )

    )

    {
  191. if

    (

    isset

    (

    $v

    [

    'value'

    ]

    )

    )
  192. $value

    =

    $v

    [

    'value'

    ]

    ;
  193. if

    (

    isset

    (

    $v

    [

    'type'

    ]

    )

    )
  194. $type

    =

    $v

    [

    'type'

    ]

    ;
  195. if

    (

    isset

    (

    $v

    [

    'compare'

    ]

    )

    )
  196. $compare

    =

    $v

    [

    'compare'

    ]

    ;
  197. }
  198. if

    (

    !

    empty

    (

    $where_data

    )

    )

    $where_data

    .=

    " {$type}

    "

    ;
  199. $where_data

    .=

    "`{$k}

    ` {$compare}

    "

    .

    (

    !

    empty

    (

    $value

    )

    ? "'{$value}

    '"

    :

    ""

    )

    ;
  200. }
  201. $select_column

    =

    ""

    ;
  202. if

    (

    !

    empty

    (

    $select

    )

    )

    {
  203. foreach

    (

    $select

    as

    $k

    =>

    $v

    )

    {
  204. if

    (

    !

    is_numeric

    (

    $v

    )

    &&

    !

    is_array

    (

    $select

    [

    $k

    ]

    )

    &&

    !

    empty

    (

    $v

    )

    )

    {
  205. $v

    =

    $this

    ->

    escapeString

    (

    addslashes

    (

    $v

    )

    )

    ;
  206. }
  207. if

    (

    is_array

    (

    $v

    )

    &&

    is_numeric

    (

    $v

    )

    &&

    empty

    (

    $v

    )

    )
  208. continue

    ;
  209. if

    (

    !

    empty

    (

    $select_column

    )

    )

    $select_column

    .=

    ", "

    ;
  210. $select_column

    .=

    "`{$v}

    `"

    ;
  211. }
  212. }

    else

    {
  213. $select_column

    =

    "*"

    ;
  214. }
  215. $select_sql

    =

    "SELECT {$select_column}

    FROM `{$tbl}

    ` "

    .

    (

    !

    empty

    (

    $where_data

    )

    ? " WHERE {$where_data}

    "

    :

    ""

    )

    ;
  216. if

    (

    !

    empty

    (

    $additional_qry

    )

    )
  217. $select_sql

    .=

    " {$additional_qry}

    "

    ;
  218. // return $select_sql;
  219. $query

    =

    $this

    ->

    query

    (

    $select_sql

    )

    ;
  220. $data

    =

    [

    ]

    ;
  221. while

    (

    $row

    =

    $query

    ->

    fetchArray

    (

    SQLITE3_ASSOC)

    )

    {
  222. $data

    [

    ]

    =

    $row

    ;
  223. }
  224. return

    !

    empty

    (

    $data

    )

    ? $data

    :

    [

    ]

    ;
  225. }
  226. }
  227. $dbcrud

    =

    new

    DBCRUD(

    "./database/test.db"

    )

    ;
  228. ?>

In the snippet above, the DBCRUD class represents the custom CRUD class that derives from the Custom SQLite Database Class called Database class. The class contains multiple methods that can be used for inserting, updating, reading/fetching, and deleting data from the database. Using the object methods, executing queries will be easier.

Object Parameters Usage

Table Parameter [$tbl]



This parameter is required for all methods listed on the object. It requires you to enter the database table name you want to manage or where you will execute the database query.

Data Parameter [$data]



This parameter is required in the insert and update method of the object. The parameter contains an array of data to insert or update on the database table. The array key must match the column name on the table and the value as the value.

Where Parameter [$where]



This parameter is required in the update, getSingle, and delete methods but optional in getResults method. It is an array of data for matching the data to manage on the table.

Array Format

  1. <?php
  2. $where

    =

    [

    "id"

    =>

    1

    ]

    ;
  3. ?>

Or

  1. <?php
  2. $where

    =

    array

    (
  3. "id"

    =>

    array

    (
  4. "value"

    =>

    1

    ,
  5. "compare"

    =>

    "="
  6. )

    ,
  7. "title"

    =>

    array

    (
  8. "value"

    =>

    "%s

    ample%"

    ,
  9. "compare"

    =>

    "LIKE"

    ,
  10. "type"

    =>

    "OR"
  11. )
  12. )

    ;
  13. ?>

Select Parameter [$select]



This parameter is an array of table column names to select.

Additional Query Statement Parameter [$additional_qry]



This parameter is a string you wish to add in getResults method query statement such as the "Order By" clause.

Example

I have provided a working application source code zip file on this site. The application is a simple application with CRUD features. It demonstrates the usage of the Custom SQLite3 CRUD Class that I provided above. The download button is located below this article's content.

Application Snapshots

Here are the snapshots of some interfaces of the sample application I have provided.

List Page

Add Form Oage

Edit Form Page

View Page

DEMO VIDEO

That's it! I hope this Creation of an SQLite3 Custom CRUD Class in PHP Tutorial will help you with what you are looking for and will be useful for your current and future PHP Projects.

Explore more on this website for more Tutorials and Free Source Codes.

Happy Coding =)


Download
You must upgrade your account or reply in the thread to view hidden text.
 

442,401

317,942

317,951

Top