{"id":379,"date":"2018-01-15T04:32:21","date_gmt":"2018-01-15T04:32:21","guid":{"rendered":"http:\/\/mddir.com\/how-to\/?p=379"},"modified":"2018-01-15T04:32:21","modified_gmt":"2018-01-15T04:32:21","slug":"php-select-data-from-mysql","status":"publish","type":"post","link":"https:\/\/www.mddir.com\/how-to\/php-select-data-from-mysql\/","title":{"rendered":"PHP Select Data From MySQL"},"content":{"rendered":"<p>You can use the php statement to get the data from MySQL database. Check the example below.<\/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 = &quot;SELECT id, firstname, lastname FROM MyGuests&quot;;<br \/>\n$result = $conn-&gt;query($sql);<\/p>\n<p>if ($result-&gt;num_rows &gt; 0) {<br \/>\n    \/\/ output data of each row<br \/>\n    while($row = $result-&gt;fetch_assoc()) {<br \/>\n        echo &quot;id: &quot; . $row[&quot;id&quot;]. &quot; &#8211; Name: &quot; . $row[&quot;firstname&quot;]. &quot; &quot; . $row[&quot;lastname&quot;]. &quot;&lt;br&gt;&quot;;<br \/>\n    }<br \/>\n} else {<br \/>\n    echo &quot;0 results&quot;;<br \/>\n}<br \/>\n$conn-&gt;close();<br \/>\n?&gt;[\/php]<\/p>\n<p><strong>Code Explanation:<\/strong><\/p>\n<p>First, we will connect to our database by using connect database php query.<\/p>\n<p><code>$servername = \"localhost\";<br \/>\n$username = \"username\";<br \/>\n$password = \"password\";<br \/>\n$dbname = \"myDB\";<\/p>\n<p>\/\/ Create connection<br \/>\n$conn = new mysqli($servername, $username, $password, $dbname);<br \/>\n\/\/ Check connection<br \/>\nif ($conn->connect_error) {<br \/>\n    die(\"Connection failed: \" . $conn->connect_error);<br \/>\n} <\/p>\n<p>Then we will run the $sql query.<br \/>\n$sql = \"SELECT id, firstname, lastname FROM MyGuests\";<br \/>\n$result = $conn->query($sql);<\/code><\/p>\n<p>In the SQL query, we are asking php script to select the id, firstname, lastname from the database MyGuests.<\/p>\n<p>On the next line we created another variable called $resut which is equal to $conn and <code>query($sql)<\/code><\/p>\n<p>Then we are running if statement.<\/p>\n<p><code>if ($result->num_rows > 0) {<br \/>\n    \/\/ output data of each row<br \/>\n    while($row = $result->fetch_assoc()) {<br \/>\n        echo \"id: \" . $row[\"id\"]. \" - Name: \" . $row[\"firstname\"]. \" \" . $row[\"lastname\"]. \"<br \/>\";<br \/>\n    }<br \/>\n} else {<br \/>\n    echo \"0 results\";<br \/>\n}<\/code><\/p>\n<p>In the if statement we have created the array with the key <code>$result<\/code> and value <code>num_row<\/code> which is bigger than 0. <\/p>\n<p>Then we have run the statement with the while function and assigned the variable $row to $result to <code>fetch_assoc()<\/code> row.<\/p>\n<p>To get the data we have created the echo statement with each individual field id, firstname and lastname. <\/p>\n","protected":false},"excerpt":{"rendered":"<p>You can use the php statement to get the data from MySQL database. Check the example below. Example Code: [php]&lt;?php $servername = &quot;localhost&quot;; $username =<\/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-379","post","type-post","status-publish","format-standard","hentry","category-php"],"_links":{"self":[{"href":"https:\/\/www.mddir.com\/how-to\/wp-json\/wp\/v2\/posts\/379","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=379"}],"version-history":[{"count":1,"href":"https:\/\/www.mddir.com\/how-to\/wp-json\/wp\/v2\/posts\/379\/revisions"}],"predecessor-version":[{"id":380,"href":"https:\/\/www.mddir.com\/how-to\/wp-json\/wp\/v2\/posts\/379\/revisions\/380"}],"wp:attachment":[{"href":"https:\/\/www.mddir.com\/how-to\/wp-json\/wp\/v2\/media?parent=379"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.mddir.com\/how-to\/wp-json\/wp\/v2\/categories?post=379"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.mddir.com\/how-to\/wp-json\/wp\/v2\/tags?post=379"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}