{"id":57363,"date":"2023-08-16T15:01:19","date_gmt":"2023-08-16T09:31:19","guid":{"rendered":"https:\/\/www.tothenew.com\/blog\/?p=57363"},"modified":"2023-09-06T15:04:40","modified_gmt":"2023-09-06T09:34:40","slug":"how-to-perform-insert-update-delete-and-select-database-queries-in-your-drupal-module","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/how-to-perform-insert-update-delete-and-select-database-queries-in-your-drupal-module\/","title":{"rendered":"How to Perform Insert, Update, Delete, and Select Database Queries in Your Drupal Module"},"content":{"rendered":"<p>If you&#8217;re building a Drupal 10 website, you&#8217;ll probably need to work with a database at some point. Whether you&#8217;re retrieving data from the database or making changes, you&#8217;ll need to use SQL queries. In this blog post, we&#8217;ll cover the basics of four common database queries in Drupal 10: insert, update, delete, and select.<\/p>\n<p>An insert query adds new data to a database table. Here&#8217;s an example of an insert query in Drupal 10:<\/p>\n<pre>$query = \\Drupal::database()-&gt;insert('ttn_mysql_table');\r\n$query-&gt;fields([ 'name' =&gt; 'Rajveer', 'age' =&gt; 30,]);\r\n$query-&gt;execute();\r\n\r\n\r\n<\/pre>\n<h4>Update Query<\/h4>\n<p>An update query changes data in an existing database table. Here&#8217;s an example of an update query in Drupal 10:<\/p>\n<pre>$query = \\Drupal::database()-&gt;update('ttn_mysql_table');\r\n$query-&gt;fields([\r\n'age' =&gt; 31,\r\n]);\r\n$query-&gt;condition('name', 'Rajveer');\r\n$query-&gt;execute();<\/pre>\n<h4>Delete Query<\/h4>\n<p>A delete query removes data from a database table. Here&#8217;s an example of a delete query in Drupal 10:<\/p>\n<div class=\"bg-black rounded-md mb-4\">\n<pre class=\"flex items-center relative text-gray-200 bg-gray-800 px-4 py-2 text-xs font-sans justify-between rounded-t-md\">$query = \\Drupal::database()-&gt;delete('ttn_mysql_table');\r\n$query-&gt;condition('age', 30);\r\n$query-&gt;execute();<\/pre>\n<\/div>\n<div>\n<h4>Select Query<\/h4>\n<p>A select query retrieves data from a database table. Here&#8217;s an example of a select query in Drupal 10:<\/p>\n<pre>$query = \\Drupal::database()-&gt;select('ttn_mysql_table', 't');\r\n$query-&gt;fields('t', ['id', 'name']);\r\n$result = $query-&gt;execute();\r\n\r\nforeach ($result as $row) {\r\n$id = $row-&gt;id;\r\n$name = $row-&gt;name;\r\n\/\/ Do something with the results.\r\n}<\/pre>\n<\/div>\n<div>\n<p>Feed your curiosity \u2013<a href=\"https:\/\/www.tothenew.com\/blog\/\">\u00a0read more<\/a>\u00a0of our insightful blogs.<\/p>\n<\/div>\n<div class=\"ap-custom-wrapper\"><\/div><!--ap-custom-wrapper-->","protected":false},"excerpt":{"rendered":"<p>If you&#8217;re building a Drupal 10 website, you&#8217;ll probably need to work with a database at some point. Whether you&#8217;re retrieving data from the database or making changes, you&#8217;ll need to use SQL queries. In this blog post, we&#8217;ll cover the basics of four common database queries in Drupal 10: insert, update, delete, and select. [&hellip;]<\/p>\n","protected":false},"author":1368,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":360},"categories":[3602,4488],"tags":[5235,4862],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/57363"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/1368"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=57363"}],"version-history":[{"count":4,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/57363\/revisions"}],"predecessor-version":[{"id":58345,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/57363\/revisions\/58345"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=57363"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=57363"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=57363"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}