{"id":216,"date":"2024-06-14T09:59:35","date_gmt":"2024-06-14T08:59:35","guid":{"rendered":"http:\/\/192.168.1.252\/?p=216"},"modified":"2024-06-16T16:52:14","modified_gmt":"2024-06-16T15:52:14","slug":"php-to-extract-latest-data-from-mysql","status":"publish","type":"post","link":"https:\/\/www.thedrift.xyz\/index.php\/2024\/06\/14\/php-to-extract-latest-data-from-mysql\/","title":{"rendered":"PHP To Extract Latest Data From MySQL"},"content":{"rendered":"<p>PHP Script uses custom CSS to generate HTML styled table to contain last nine database rows from the vehicle speed system.<\/p>\n<ul>\n<li>Converts MySQL DateTime entry\n<ul>\n<li>YYYY\/MM\/DD H:i:s:xxx to h:i:s<\/li>\n<\/ul>\n<\/li>\n<li>Converts Direction Integer to Text\n<ul>\n<li>1 = Up, 2=Down<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p>$servername = &#8220;xxxxxxx&#8221;;<br \/>\n$username = &#8220;xxx&#8221;;<br \/>\n$password = &#8220;xxxxx&#8221;;<br \/>\n$dbname = &#8220;xxxxx&#8221;;<\/p>\n<p>\/\/ Create connection<br \/>\n$conn = new mysqli($servername, $username, $password, $dbname);<br \/>\n\/\/ Check connection<br \/>\nif ($conn-&gt;connect_error) {<br \/>\ndie(&#8220;Connection failed: &#8221; . $conn-&gt;connect_error);<br \/>\n}<\/p>\n<p>$sql = &#8220;SELECT * FROM traffic ORDER BY Time DESC LIMIT 9&#8221;;<br \/>\n$result = $conn-&gt;query($sql);<\/p>\n<p>if ($result-&gt;num_rows &gt; 0) {<br \/>\necho &#8220;&lt;table class=styled-table&gt;&#8221;;<br \/>\necho &#8220;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Time&lt;\/th&gt;&lt;th&gt;Speed&lt;\/th&gt;&lt;th&gt;Direction&lt;\/th&gt;&lt;th&gt;Count&lt;\/th&gt;&lt;th&gt;SD&lt;\/th&gt;&lt;\/tr&gt;&lt;\/thead&gt;&#8221;;<br \/>\n\/\/ output data of each row<br \/>\necho &#8220;&lt;tbody&gt;&#8221;;<br \/>\nwhile($row = $result-&gt;fetch_assoc()) {<\/p>\n<p>$secvar = &#8220;&#8221;;<br \/>\n{$secvar = strtotime((trim($row[&#8220;Time&#8221;])));}<br \/>\n{$secvar2 = date(&#8220;H:i:s&#8221;, $secvar);}<br \/>\n$firstvar = &#8220;&#8221;;<br \/>\nif (trim($row[&#8216;Direction&#8217;]) ==1)<br \/>\n{$firstvar = &#8220;Up&#8221;;}<br \/>\nif (trim($row[&#8216;Direction&#8217;]) ==2)<br \/>\n{$firstvar = &#8220;Down&#8221;;}<br \/>\necho &#8220;&lt;tr&gt;&lt;td&gt;&#8221;.$secvar2.&#8221;&lt;\/td&gt;&lt;td&gt;&#8221;.$row[&#8220;Speed&#8221;].&#8221;&lt;\/td&gt;&lt;td&gt;&#8221;.$firstvar.&#8221;&lt;\/td&gt;&lt;td&gt;&#8221;.$row[&#8220;Count&#8221;].&#8221;&lt;\/td&gt;&lt;td&gt;&#8221;.$row[&#8220;SD&#8221;].&#8221;&lt;\/td&gt;&lt;\/tr&gt;&#8221;;<br \/>\n}<br \/>\necho &#8220;&lt;\/tbody&gt;&#8221;;<br \/>\necho &#8220;&lt;\/table&gt;&#8221;;<br \/>\n} else {<br \/>\necho &#8220;0 results&#8221;;<br \/>\n}<br \/>\n$conn-&gt;close();<\/p>\n","protected":false},"excerpt":{"rendered":"<p>PHP Script uses custom CSS to generate HTML styled table to contain last nine database rows from the vehicle speed system. Converts MySQL DateTime entry YYYY\/MM\/DD H:i:s:xxx to h:i:s Converts Direction Integer to Text 1 = Up, 2=Down $servername = &#8220;xxxxxxx&#8221;; $username = &#8220;xxx&#8221;; $password = &#8220;xxxxx&#8221;; $dbname = &#8220;xxxxx&#8221;; \/\/ Create connection $conn = &hellip; <a href=\"https:\/\/www.thedrift.xyz\/index.php\/2024\/06\/14\/php-to-extract-latest-data-from-mysql\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;PHP To Extract Latest Data From MySQL&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[19,16],"tags":[],"class_list":["post-216","post","type-post","status-publish","format-standard","hentry","category-raspberry-pi","category-raspberry-pi-server"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.thedrift.xyz\/index.php\/wp-json\/wp\/v2\/posts\/216","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.thedrift.xyz\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.thedrift.xyz\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.thedrift.xyz\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.thedrift.xyz\/index.php\/wp-json\/wp\/v2\/comments?post=216"}],"version-history":[{"count":2,"href":"https:\/\/www.thedrift.xyz\/index.php\/wp-json\/wp\/v2\/posts\/216\/revisions"}],"predecessor-version":[{"id":220,"href":"https:\/\/www.thedrift.xyz\/index.php\/wp-json\/wp\/v2\/posts\/216\/revisions\/220"}],"wp:attachment":[{"href":"https:\/\/www.thedrift.xyz\/index.php\/wp-json\/wp\/v2\/media?parent=216"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.thedrift.xyz\/index.php\/wp-json\/wp\/v2\/categories?post=216"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.thedrift.xyz\/index.php\/wp-json\/wp\/v2\/tags?post=216"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}