{"id":1434,"date":"2016-05-02T20:58:53","date_gmt":"2016-05-03T01:58:53","guid":{"rendered":"http:\/\/xlinesoft.com\/blog\/?p=1434"},"modified":"2016-05-21T20:44:18","modified_gmt":"2016-05-22T01:44:18","slug":"troubleshooting-sql-queries","status":"publish","type":"post","link":"https:\/\/xlinesoft.com\/blog\/2016\/05\/02\/troubleshooting-sql-queries\/","title":{"rendered":"Troubleshooting SQL queries"},"content":{"rendered":"<p>Web applications generated by PHPRunner, ASPRunner.NET or ASPRunnerPro communicate with databases via means of SQL queries. Whenever you search, edit or delete data your web application issues a series of SQL queries, gets results back and displays it on the web page. Understanding the basics of SQL will help you build better apps and find errors faster. <\/p>\n<p>Our code generators come will handy option to display all SQL queries application executes. For this purpose you can add the following line of code to AfterApplicationInitialized event:<\/p>\n<p>In PHPRunner<\/p>\n<pre name=\"code\" class=\"php:nocontrols\">\r\n$dDebug = true;\r\n<\/pre>\n<p><!--more--><br \/>\nIn ASPRunner.NET (C#)<\/p>\n<pre  name=\"code\" class=\"csharp:nocontrols\">\r\nGlobalVars.dDebug = true;\r\n<\/pre>\n<p>In ASPRunnerPro<\/p>\n<pre  name=\"code\" class=\"vb:nocontrols\">\r\ndDebug = true\r\n<\/pre>\n<p>Lets see how this option can help us troubleshoot your web applications. <\/p>\n<p>1. Troubleshooting Advanced Security mode.<\/p>\n<p>Consider a real life example. Customer opens a ticket that Advanced Security mode &#8220;Users can see and edit their own data only&#8221; doesn&#8217;t work. Customers are supposed to see their own orders only but see all of them.<\/p>\n<p>Turning on SQL debugging shows us the following:<\/p>\n<pre>\r\nselect count(*) FROM `orders` \r\nSELECT `OrderID`,`CustomerID`,`EmployeeID`,`OrderDate` FROM `orders` ORDER BY 1 ASC\r\n<\/pre>\n<p>First query calculates number of records that matches the current search criteria. Second query actually retrieves the data. As you can see there is no WHERE clause that restricts data to the current logged user. All orders are shown.<\/p>\n<p>After checking project settings we figured out that all users were accidentally added to the Admin group (admin users can see all data). Once fixed, correct SQL query is produced and only orders that belong to ANATR customer are displayed. <\/p>\n<pre>\r\nselect count(*) FROM `orders` where (CustomerID='ANATR')\r\nSELECT `OrderID`,`CustomerID`,`EmployeeID`,`OrderDate` FROM `orders` where (CustomerID='ANATR') ORDER BY 1 ASC\r\n<\/pre>\n<p>2. Troubleshooting Master-Details in AJAX mode<\/p>\n<p>Master-details drill-down functionality is implemented via AJAX. AJAX requests are send to the server behind the scene and usually response is expected in very specific format (JSON). Adding anything to the output like SQL queries will break master-details functionality however we still can see SQL queries that retrieve details.<\/p>\n<p>For instance, we link Orders and &#8216;Order Details&#8217; as Master-Details. If we enable SQL query debugging mode and try to expand details we get error message as follows. <\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/xlinesoft.com\/blog\/wp-content\/uploads\/2016\/05\/sql_debugging_1.png\" alt=\"\" title=\"sql_debugging_1\" width=\"745\" height=\"534\" class=\"alignnone size-full wp-image-1446\" srcset=\"https:\/\/xlinesoft.com\/blog\/wp-content\/uploads\/2016\/05\/sql_debugging_1.png 745w, https:\/\/xlinesoft.com\/blog\/wp-content\/uploads\/2016\/05\/sql_debugging_1-300x215.png 300w, https:\/\/xlinesoft.com\/blog\/wp-content\/uploads\/2016\/05\/sql_debugging_1-150x107.png 150w, https:\/\/xlinesoft.com\/blog\/wp-content\/uploads\/2016\/05\/sql_debugging_1-400x286.png 400w\" sizes=\"auto, (max-width: 745px) 100vw, 745px\" \/><\/p>\n<p>However if we click &#8216;See details&#8217; link we will see the same two SQL queries that retrieve data from &#8216;Order Details&#8217; table. <\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/xlinesoft.com\/blog\/wp-content\/uploads\/2016\/05\/sql_debugging_2.png\" alt=\"\" title=\"sql_debugging_2\" width=\"745\" height=\"534\" class=\"alignnone size-full wp-image-1445\" srcset=\"https:\/\/xlinesoft.com\/blog\/wp-content\/uploads\/2016\/05\/sql_debugging_2.png 745w, https:\/\/xlinesoft.com\/blog\/wp-content\/uploads\/2016\/05\/sql_debugging_2-300x215.png 300w, https:\/\/xlinesoft.com\/blog\/wp-content\/uploads\/2016\/05\/sql_debugging_2-150x107.png 150w, https:\/\/xlinesoft.com\/blog\/wp-content\/uploads\/2016\/05\/sql_debugging_2-400x286.png 400w\" sizes=\"auto, (max-width: 745px) 100vw, 745px\" \/><\/p>\n<p>3. Troubleshooting buttons<\/p>\n<p>Troubleshooting button&#8217;s code that executes SQL queries is a bit more trickier. We are going to dig a little deeper using Developers Tools in Chrome web browser.<\/p>\n<p>For instance, we have added a button to Orders grid that copies selected order to OrdersArchive table. Here is our Server code (PHP). <\/p>\n<pre name=\"code\" class=\"php:nocontrols\">\r\n$record = $button->getCurrentRecord();\r\n$sql = \"insert into OrdersArchive (OrderID, OrderDate, CustomerID) \r\nvalues (\".$record[\"OrderID\"].\",'\".$record[\"OrderDate\"].\"',\".$record[\"CustomerID\"].\")\";\r\nCustomQuery($sql);\r\n<\/pre>\n<p>For some reason this button doesn&#8217;t work, no records appear in OrdersArchive table when we click it. We need to make sure that our SQL query is correct.<\/p>\n<p>First step is to open Chrome Developer Tools by clicking F12. Similar tools also exist in Firefox and Internet Explorer and F12 is the common hot key to open developers console. <\/p>\n<p>Go to &#8216;Network&#8217; tab. Click &#8216;Copy to archive&#8217; button. You can see a request being sent to buttonhandler.php file. <\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/xlinesoft.com\/blog\/wp-content\/uploads\/2016\/05\/sql_debugging_3.png\" alt=\"\" title=\"sql_debugging_3\" width=\"736\" height=\"495\" class=\"alignnone size-full wp-image-1455\" srcset=\"https:\/\/xlinesoft.com\/blog\/wp-content\/uploads\/2016\/05\/sql_debugging_3.png 736w, https:\/\/xlinesoft.com\/blog\/wp-content\/uploads\/2016\/05\/sql_debugging_3-300x201.png 300w, https:\/\/xlinesoft.com\/blog\/wp-content\/uploads\/2016\/05\/sql_debugging_3-150x100.png 150w, https:\/\/xlinesoft.com\/blog\/wp-content\/uploads\/2016\/05\/sql_debugging_3-400x269.png 400w\" sizes=\"auto, (max-width: 736px) 100vw, 736px\" \/><\/p>\n<p>Click &#8216;buttonhandler.php&#8217; and go to &#8216;Preview&#8217; tab. You can see the error message there (<strong>Unknown column &#8216;ANATR&#8217; in &#8216;field list&#8217;<\/strong>) along with complete SQL Query. <\/p>\n<p><a href=\"http:\/\/xlinesoft.com\/blog\/wp-content\/uploads\/2016\/05\/sql_debugging_4.png\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/xlinesoft.com\/blog\/wp-content\/uploads\/2016\/05\/sql_debugging_4-300x149.png\" alt=\"\" title=\"sql_debugging_4\" width=\"300\" height=\"149\" class=\"alignnone size-medium wp-image-1454\" srcset=\"https:\/\/xlinesoft.com\/blog\/wp-content\/uploads\/2016\/05\/sql_debugging_4-300x149.png 300w, https:\/\/xlinesoft.com\/blog\/wp-content\/uploads\/2016\/05\/sql_debugging_4-1024x510.png 1024w, https:\/\/xlinesoft.com\/blog\/wp-content\/uploads\/2016\/05\/sql_debugging_4-150x74.png 150w, https:\/\/xlinesoft.com\/blog\/wp-content\/uploads\/2016\/05\/sql_debugging_4-400x199.png 400w, https:\/\/xlinesoft.com\/blog\/wp-content\/uploads\/2016\/05\/sql_debugging_4.png 1300w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p>Probably you have spotted the error already. The text value of <strong>ANATR<\/strong> must be wrapped by single quotes like this: <strong>&#8216;ANATR&#8217;<\/strong>.<\/p>\n<p>Here is how we need to fix our code:<\/p>\n<pre name=\"code\" class=\"php:nocontrols\">\r\n$record = $button->getCurrentRecord();\r\n$sql = \"insert into OrdersArchive (OrderID, OrderDate, CustomerID) \r\nvalues (\".$record[\"OrderID\"].\",'\".$record[\"OrderDate\"].\"','\".$record[\"CustomerID\"].\"')\";\r\nCustomQuery($sql);\r\n<\/pre>\n<p>Better yet, we can use DAL which gives us cleaner code and also takes care of quoting automatically:<\/p>\n<pre name=\"code\" class=\"php:nocontrols\">\r\nglobal $dal;\r\n$record = $button->getCurrentRecord();\r\n\r\n$tblArchive = $dal->Table(\"OrdersArchive\");\r\n$tblArchive->Value[\"OrderID\"]=$record[\"OrderID\"];\r\n$tblArchive->Value[\"OrderDate\"]=$record[\"OrderDate\"];\r\n$tblArchive->Value[\"CustomerID\"]=$record[\"CustomerID\"];\r\n\r\n$tblArchive->Add();\r\n<\/pre>\n<p>And the same code for ASPRunner.NET:<\/p>\n<pre name=\"code\" class=\"csharp:nocontrols\">\r\ndynamic record = button.getCurrentRecord();\r\ndynamic tblArchive = GlobalVars.dal.Table(\"OrdersArchive\");\r\ntblArchive.Value[\"OrderID\"] = record[\"OrderID\"];\r\ntblArchive.Value[\"OrderDate\"] = record[\"OrderDate\"];\r\ntblArchive.Value[\"CustomerID\"] = record[\"CustomerID\"];\r\ntblArchive.Add();\r\n<\/pre>\n<p>And for ASPRunnerPro:<\/p>\n<pre name=\"code\" class=\"vb:nocontrols\">\r\nDoAssignment record, button.getCurrentRecord()\r\n\r\ndal.Table(\"OrdersArchive\").Value(\"OrderID\")=record(\"OrderID\")\r\ndal.Table(\"OrdersArchive\").Value(\"OrderDate\")=record(\"OrderDate\")\r\ndal.Table(\"OrdersArchive\").Value(\"CustomerID\")=record(\"CustomerID\")\r\ndal.Table(\"OrdersArchive\").Add()\r\n\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Web applications generated by PHPRunner, ASPRunner.NET or ASPRunnerPro communicate with databases via means of SQL queries. Whenever you search, edit or delete data your web application issues a series of SQL queries, gets results back and displays it on the web page. Understanding the basics of SQL will help you build better apps and find errors faster. Our code generators come will handy option to display all SQL queries application executes. For this purpose you can add the following line of code to AfterApplicationInitialized event:&#8230;<span class=\"clearfix clearfix-post\"><\/span><a href=\"https:\/\/xlinesoft.com\/blog\/2016\/05\/02\/troubleshooting-sql-queries\/\" class=\"more-link\">Continue Reading <span class=\"screen-reader-text\">&#8220;Troubleshooting SQL queries&#8221;<\/span> <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16,1,4,3,8],"tags":[],"class_list":["post-1434","post","type-post","status-publish","format-standard","hentry","category-asp-net","category-php-category","category-php-code-generator","category-php-form-generator","category-tutorials"],"_links":{"self":[{"href":"https:\/\/xlinesoft.com\/blog\/wp-json\/wp\/v2\/posts\/1434","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/xlinesoft.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/xlinesoft.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/xlinesoft.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/xlinesoft.com\/blog\/wp-json\/wp\/v2\/comments?post=1434"}],"version-history":[{"count":20,"href":"https:\/\/xlinesoft.com\/blog\/wp-json\/wp\/v2\/posts\/1434\/revisions"}],"predecessor-version":[{"id":1467,"href":"https:\/\/xlinesoft.com\/blog\/wp-json\/wp\/v2\/posts\/1434\/revisions\/1467"}],"wp:attachment":[{"href":"https:\/\/xlinesoft.com\/blog\/wp-json\/wp\/v2\/media?parent=1434"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/xlinesoft.com\/blog\/wp-json\/wp\/v2\/categories?post=1434"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/xlinesoft.com\/blog\/wp-json\/wp\/v2\/tags?post=1434"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}