{"id":1402,"date":"2017-01-19T01:00:14","date_gmt":"2017-01-19T01:00:14","guid":{"rendered":"http:\/\/www.fastwebhost.in\/blog\/?p=1402"},"modified":"2019-01-23T06:09:32","modified_gmt":"2019-01-23T06:09:32","slug":"establishing-a-database-connection-using-php","status":"publish","type":"post","link":"https:\/\/www.fastwebhost.in\/blog\/establishing-a-database-connection-using-php\/","title":{"rendered":"Establishing a Database Connection Using PHP"},"content":{"rendered":"<p>In this&nbsp;tutorial,&nbsp;we&#8217;ll show how to establish a database connection to your MySQL database using PHP.<\/p>\n<p>PHP and MySQL is one of the most common stacks for web development. With it such a common combination powering so many websites.<\/p>\n<h5>Database Connection settings<\/h5>\n<p>To be able to connect to a MySQL database, you need to update the following MySQL Database Connection Credentials.<\/p>\n<p>Before that make sure to create a Database name, Database username\u200b and Password for your database user.&nbsp;<\/p>\n<p>Database Name, Database USER and Database Password can be created from your cPanel or from shell.\u200b<\/p>\n<p>DATABASE NAME, DATABASE USER, PASSWORD, HOSTNAME<\/p>\n<p>Now,&nbsp;you&#8217;ve&nbsp;to create a sample.php file and copy the below code and replace the fields with your DATABASE Credentials. The below is how it looks in your terminal and the code follows.<\/p>\n<p><span><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/content.fastwebhost.in\/wp-content\/uploads\/2017\/01\/php_codee-300x207.png\" alt=\"Database connection code in terminal\" width=\"339\" height=\"234\" style=\"width: 339px;\"><\/span><\/p>\n<p>================================================&lt;?php<\/p>\n<p>\/\/ENTER YOUR DATABASE CONNECTION INFO BELOW:<\/p>\n<p>$hostname=&#8221;localhost&#8221;;$database=&#8221;test_db&#8221;;$username=&#8221;test_user&#8221;;$password=&#8221;qwe234@#$&#8221;;<\/p>\n<p>\/\/DO NOT EDIT BELOW THIS LINE$link = mysql_connect($hostname, $username, $password);if (!$link) {die(&#8216;Connection failed: &#8216; . mysql_error());}else{echo &#8220;Host SUCCESS! &#8221; .$hostname . &#8220;&#8221; . PHP_EOL;}<\/p>\n<p>$db_selected = mysql_select_db($database, $link);if (!$db_selected) {die (&#8216;Can&#8217;t select database: &#8216; . mysql_error());}else {echo &#8216;You have successfully established a connection to your DB &#8216; . $database . &#8221;;}<\/p>\n<p>mysql_close($link);<\/p>\n<p>?&gt;=============================================<\/p>\n<p>Just RUN this sample.php files in your browser and you can see the below result on browser.<\/p>\n<blockquote><p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;http:\/\/IP\/sample.php<\/p><\/blockquote>\n<p>Host SUCCESS! localhost You have successfully established a connection to your DB test_db<\/p>\n<h5>Conclusion<\/h5>\n<p>Finally, after complete reading this article you can surely connect your database using PHP.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this&nbsp;tutorial,&nbsp;we&#8217;ll show how to establish a database connection to your MySQL database using PHP. PHP and MySQL is one of the most common stacks for web development. With it such a common combination powering so many websites. Database Connection settings To be able to connect to a MySQL database, you need to update the [&hellip;]<\/p>\n","protected":false},"author":6,"featured_media":1432,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"_uf_show_specific_survey":0,"_uf_disable_surveys":false,"footnotes":""},"categories":[80],"tags":[57,36,65],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.fastwebhost.in\/blog\/wp-json\/wp\/v2\/posts\/1402"}],"collection":[{"href":"https:\/\/www.fastwebhost.in\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.fastwebhost.in\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.fastwebhost.in\/blog\/wp-json\/wp\/v2\/users\/6"}],"replies":[{"embeddable":true,"href":"https:\/\/www.fastwebhost.in\/blog\/wp-json\/wp\/v2\/comments?post=1402"}],"version-history":[{"count":3,"href":"https:\/\/www.fastwebhost.in\/blog\/wp-json\/wp\/v2\/posts\/1402\/revisions"}],"predecessor-version":[{"id":4732,"href":"https:\/\/www.fastwebhost.in\/blog\/wp-json\/wp\/v2\/posts\/1402\/revisions\/4732"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.fastwebhost.in\/blog\/wp-json\/wp\/v2\/media\/1432"}],"wp:attachment":[{"href":"https:\/\/www.fastwebhost.in\/blog\/wp-json\/wp\/v2\/media?parent=1402"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.fastwebhost.in\/blog\/wp-json\/wp\/v2\/categories?post=1402"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.fastwebhost.in\/blog\/wp-json\/wp\/v2\/tags?post=1402"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}