{"id":397,"date":"2021-06-10T14:14:34","date_gmt":"2021-06-10T13:14:34","guid":{"rendered":"https:\/\/blogs.glowscotland.org.uk\/sh\/ahscomputinghtml\/?page_id=397"},"modified":"2021-06-10T15:50:51","modified_gmt":"2021-06-10T14:50:51","slug":"php-and-sql","status":"publish","type":"page","link":"https:\/\/blogs.glowscotland.org.uk\/sh\/ahscomputinghtml\/php\/php-and-sql\/","title":{"rendered":"PHP and SQL"},"content":{"rendered":"<p><img loading=\"lazy\" decoding=\"async\" class=\"alignright wp-image-196\" src=\"https:\/\/blogs.glowscotland.org.uk\/sh\/public\/ahscomputinghtml\/uploads\/sites\/3275\/2021\/06\/05211152\/sqa-ah.png\" alt=\"\" width=\"100\" height=\"97\" \/><a href=\"https:\/\/www.w3schools.com\/pHp\/php_mysql_intro.asp\" target=\"_blank\" rel=\"noopener noreferrer\"><img loading=\"lazy\" decoding=\"async\" class=\"image-59 alignright\" src=\"https:\/\/glow-prod-sh.s3.eu-west-1.amazonaws.com\/sh\/public\/ahscomputinghtml\/uploads\/sites\/3275\/2021\/06\/03210120\/w3schools-150x150.png?X-Amz-Content-Sha256=UNSIGNED-PAYLOAD&#038;X-Amz-Security-Token=IQoJb3JpZ2luX2VjEMT%2F%2F%2F%2F%2F%2F%2F%2F%2F%2FwEaCWV1LXdlc3QtMSJGMEQCIFIuagJ%2FQbsh6cJaMwxxX26KpyAoAgPrYqCigWyoOv%2B5AiB7WZPdXsqs%2Fi%2Bmu5YCNfh48i%2B67qkp435SEsAUxInvzirABQiN%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F8BEAUaDDI3MzU3MDE5NTQzMyIMJTyQuVvRf9Jge%2FeDKpQFz5Cc9c1q02tUEX5LxaEp%2F%2F1ajRx9LvgS3wKuFWThN5opxKrYkwBv9hrpUDwj9DB2NjkX9H3suTRhZR5yWxdaXPtupByCZQCjDdjqs0SA%2F1OnO2KGge%2BhtwnAJnkqpBiO2V4ygXkvG1Qh1RqtNjjR625o7DaT16ySk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A256&#038;X-Amz-Credential=ASIAT7MQN47UVPVFLOWC%2F20260516%2Feu-west-1%2Fs3%2Faws4_request&#038;X-Amz-Date=20260516T121548Z&#038;X-Amz-SignedHeaders=host&#038;X-Amz-Expires=900&#038;X-Amz-Signature=ca926207a7364384be6d83e00aae9c1a483f7205bafbe4f43e65a24e11a6dfaf\" alt=\"\" width=\"50\" height=\"47\" \/><\/a>PHP is often to use form data to read from, or update, SQL databases.<\/p>\n<h1>Database Connection<\/h1>\n<p>Remember the order of parameters &#8211; you connect to the <strong>server<\/strong> with your <strong>name<\/strong> and <strong>password.<\/strong> Then you select the <strong>database<\/strong> to be used.<\/p>\n<p>In this example, three possible responses are given to a connection failure &#8211; it would be unlikely to use all three.<\/p>\n<pre class=\"brush: php; title: PHP; notranslate\" title=\"PHP\">\r\n$servername = &quot;localhost&quot;;\r\n$username = &quot;root&quot;;\r\n$password = &quot;&quot;;\r\n$dbname = &quot;studentlist&quot;;\r\n\r\n$conn = mysqli_connect($servername, $username, $password, $dbname);\r\n\r\nif (!$conn) {\r\n    echo '&lt;img src=&quot;data:image\/gif;base64,R0lGODlhAQABAIAAAAAAAP\/\/\/yH5BAEAAAAALAAAAAABAAEAAAIBRAA7&quot; data-wp-preserve=&quot;%3Cscript%3Ealert(%22Error%20inserting%20data%22)%3B%3C%2Fscript%3E&quot; data-mce-resize=&quot;false&quot; data-mce-placeholder=&quot;1&quot; class=&quot;mce-object&quot; width=&quot;20&quot; height=&quot;20&quot; alt=&quot;&amp;lt;script&amp;gt;&quot; title=&quot;&amp;lt;script&amp;gt;&quot; \/&gt;'; \/\/ pop-up message\r\n    &lt;p&gt;Error inserting data&lt;\/p&gt; die(&quot;Error adding data&quot;);   \/\/ error on page\r\n    die(&quot;Connection to database failed&quot;);                   \/\/ halt script\r\n}\r\n<\/pre>\n<p>At the end of the script, the database connection should be closed:<\/p>\n<pre class=\"brush: php; title: PHP; notranslate\" title=\"PHP\">\r\nmysqli_close($conn);\r\n<\/pre>\n<h1>SQL Insert\/Update\/Delete<\/h1>\n<p>These SQL queries retrun a Boolean True\/False result that can be used to check for success:<\/p>\n<pre class=\"brush: php; title: PHP; notranslate\" title=\"PHP\">\r\n$sql = &quot;INSERT INTO students VALUES ($forename, $surname);\r\n$result = mysqli_query($conn, $sql);\r\nif (!result) { \r\n    echo '&lt;img src=&quot;data:image\/gif;base64,R0lGODlhAQABAIAAAAAAAP\/\/\/yH5BAEAAAAALAAAAAABAAEAAAIBRAA7&quot; data-wp-preserve=&quot;%3Cscript%3Ealert(%22Error%20inserting%20data%22)%3B%3C%2Fscript%3E&quot; data-mce-resize=&quot;false&quot; data-mce-placeholder=&quot;1&quot; class=&quot;mce-object&quot; width=&quot;20&quot; height=&quot;20&quot; alt=&quot;&amp;lt;script&amp;gt;&quot; title=&quot;&amp;lt;script&amp;gt;&quot; \/&gt;';\r\n}\r\n<\/pre>\n<h1>SQL Select<\/h1>\n<p>Select queries return will 0, 1 or more records:<\/p>\n<pre class=\"brush: php; title: PHP; notranslate\" title=\"PHP\">\r\n$sql = &quot;SELECT forename, surname FROM students&quot;;\r\n$result = mysqli_query($conn, $sql);\r\n \r\n$studentsFound = mysqli_num_rows($result);\r\nif ($studentsFound &gt;= 0) {\r\n    echo &quot;&lt;table&gt;&quot;;\r\n    while($row = mysqli_fetch_array($result)) {\r\n        echo &quot;&lt;tr&gt;&lt;td&quot; . $row&#x5B;&quot;forename&quot;] . &quot;&lt;\/td&gt;&lt;td&gt;&quot; . $row&#x5B;&quot;surname&quot;] . &quot;&lt;\/td&gt;&lt;\/tr&gt;&quot;;\r\n    echo &quot;\/table&gt;&quot;;\r\n} else {\r\n    echo &quot;&lt;p&gt;No results found&lt;\/p&gt;&quot;\r\n}\r\n\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>PHP is often to use form data to read from, or update, SQL databases. Database Connection Remember the order of parameters &#8211; you connect to the server with your name and password. Then you select the database to be used. In this example, three possible responses are given to a connection failure &#8211; it would be unlikely to use all three. $servername = &quot;localhost&quot;; $username = &quot;root&quot;; $password = &quot;&quot;;<\/p>\n<p><a class=\"more-link\" href=\"https:\/\/blogs.glowscotland.org.uk\/sh\/ahscomputinghtml\/php\/php-and-sql\/\">Read More<\/a><\/p>\n","protected":false},"author":7,"featured_media":0,"parent":343,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-397","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/blogs.glowscotland.org.uk\/sh\/ahscomputinghtml\/wp-json\/wp\/v2\/pages\/397","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blogs.glowscotland.org.uk\/sh\/ahscomputinghtml\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/blogs.glowscotland.org.uk\/sh\/ahscomputinghtml\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.glowscotland.org.uk\/sh\/ahscomputinghtml\/wp-json\/wp\/v2\/users\/7"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.glowscotland.org.uk\/sh\/ahscomputinghtml\/wp-json\/wp\/v2\/comments?post=397"}],"version-history":[{"count":9,"href":"https:\/\/blogs.glowscotland.org.uk\/sh\/ahscomputinghtml\/wp-json\/wp\/v2\/pages\/397\/revisions"}],"predecessor-version":[{"id":424,"href":"https:\/\/blogs.glowscotland.org.uk\/sh\/ahscomputinghtml\/wp-json\/wp\/v2\/pages\/397\/revisions\/424"}],"up":[{"embeddable":true,"href":"https:\/\/blogs.glowscotland.org.uk\/sh\/ahscomputinghtml\/wp-json\/wp\/v2\/pages\/343"}],"wp:attachment":[{"href":"https:\/\/blogs.glowscotland.org.uk\/sh\/ahscomputinghtml\/wp-json\/wp\/v2\/media?parent=397"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}