{"id":381,"date":"2018-01-15T04:37:42","date_gmt":"2018-01-15T04:37:42","guid":{"rendered":"http:\/\/mddir.com\/how-to\/?p=381"},"modified":"2018-01-15T04:37:42","modified_gmt":"2018-01-15T04:37:42","slug":"php-delete-data-from-mysql","status":"publish","type":"post","link":"https:\/\/www.mddir.com\/how-to\/php-delete-data-from-mysql\/","title":{"rendered":"PHP Delete Data From MySQL"},"content":{"rendered":"<p>You can use php query to delete the data from the mysql database.<\/p>\n<p><strong>Example code:<\/strong><\/p>\n<p>[php]&lt;?php<br \/>\n$servername = &quot;localhost&quot;;<br \/>\n$username = &quot;username&quot;;<br \/>\n$password = &quot;password&quot;;<br \/>\n$dbname = &quot;myDB&quot;;<\/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 \/>\n    die(&quot;Connection failed: &quot; . $conn-&gt;connect_error);<br \/>\n} <\/p>\n<p>\/\/ sql to delete a record<br \/>\n$sql = &quot;DELETE FROM MyGuests WHERE id=3&quot;;<\/p>\n<p>if ($conn-&gt;query($sql) === TRUE) {<br \/>\n    echo &quot;Record deleted successfully&quot;;<br \/>\n} else {<br \/>\n    echo &quot;Error deleting record: &quot; . $conn-&gt;error;<br \/>\n}<\/p>\n<p>$conn-&gt;close();<br \/>\n?&gt;[\/php]<\/p>\n<p><strong>Code Explanation:<\/strong><\/p>\n<p>After we connected to the database. We have used <code>$sql<\/code> query to instruct the php script to delete the data from <code>myGuest<\/code> database <code>WHERE <\/code>id number is 3.<\/p>\n<p><code>$sql = \"DELETE FROM MyGuests WHERE id=3\";<\/code><\/p>\n<p>On the next statement, we have used if statement to check whether php script has successfully executed or not. if it resulted true then echo statement delete data will be executed.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>You can use php query to delete the data from the mysql database. Example code: [php]&lt;?php $servername = &quot;localhost&quot;; $username = &quot;username&quot;; $password = &quot;password&quot;;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8],"tags":[],"class_list":["post-381","post","type-post","status-publish","format-standard","hentry","category-php"],"_links":{"self":[{"href":"https:\/\/www.mddir.com\/how-to\/wp-json\/wp\/v2\/posts\/381","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.mddir.com\/how-to\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.mddir.com\/how-to\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.mddir.com\/how-to\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.mddir.com\/how-to\/wp-json\/wp\/v2\/comments?post=381"}],"version-history":[{"count":1,"href":"https:\/\/www.mddir.com\/how-to\/wp-json\/wp\/v2\/posts\/381\/revisions"}],"predecessor-version":[{"id":382,"href":"https:\/\/www.mddir.com\/how-to\/wp-json\/wp\/v2\/posts\/381\/revisions\/382"}],"wp:attachment":[{"href":"https:\/\/www.mddir.com\/how-to\/wp-json\/wp\/v2\/media?parent=381"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.mddir.com\/how-to\/wp-json\/wp\/v2\/categories?post=381"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.mddir.com\/how-to\/wp-json\/wp\/v2\/tags?post=381"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}