{"id":8711,"date":"2020-11-30T10:46:23","date_gmt":"2020-11-30T10:46:23","guid":{"rendered":"https:\/\/www.milesweb.co.uk\/hosting-faqs\/?p=8711"},"modified":"2020-11-30T10:47:18","modified_gmt":"2020-11-30T10:47:18","slug":"connect-to-mysql-with-the-use-of-php","status":"publish","type":"post","link":"https:\/\/www.milesweb.co.uk\/hosting-faqs\/connect-to-mysql-with-the-use-of-php\/","title":{"rendered":"How to Connect to MySQL with the use of PHP?"},"content":{"rendered":"<p>In this article, you will learn to connect to a MySQL database using PHP with the below methods:<\/p>\n<div class=\"skrlto-container\">\n<div class=\"skrlto-header-title\"><strong>Table of Contents<\/strong><\/div>\n<div class=\"skrlto-links-wrapper\">\n<div class=\"skroll-button\" data-skrolllto=\"1ConnecttoMySQL\">1. Method #1: Connect to MySQL with MySQL Improved<\/div>\n<div class=\"skroll-button\" data-skrolllto=\"2ConnecttoMySQL\">2. Method #2: Connect to MySQL using PHP Data Objects (PDO)<\/div>\n<div class=\"skroll-button\" data-skrolllto=\"3ConnecttoMySQL\">3. Method #3: Connect to MySQL Using Legacy PHP Functions<\/div>\n<div class=\"skroll-button\" data-skrolllto=\"4ConnecttoMySQL\">4. Method #4: Connect to Remote MySQL Databases with the use of PHP<\/div>\n<\/div>\n<\/div>\n<h3 id=\"1ConnecttoMySQL\">Method #1: Connect to MySQL with MySQL Improved<\/h3>\n<p>The MySQL Improved extension using the mysqli class replaces the set of legacy MySQL functions.<\/p>\n<p><strong>For connecting to MySQL with the MySQL Improved extension, follow these steps:<\/strong><\/p>\n<p>Use the below PHP code to connect to MySQL and select a database. Change the username, password, and dbname as per yours:<br \/>\n<!--?php $mysqli = new mysqli(\"localhost\", \"username\", \"password\", \"dbname\"); ?--><\/p>\n<pre class=\"code\">&lt;?php\r\n    mysql_connect('localhost','<span class=\"variable\"><em>username<\/em><\/span>','<span class=\"variable\"><em>password<\/em><\/span>');mysql_select_db(\"<span class=\"variable\"><em>dbname<\/em><\/span>\");\r\n?&gt;<\/pre>\n<p>Once the code connects to MySQL and selects the database, you can run SQL queries and perform other operations. For instance, the below PHP code runs a SQL query that displays the last names from the employees table, and stores the result in the $result variable:<br \/>\n<!--?php $result = $mysqli-&gt;query(\"SELECT lastname FROM employees\");&lt;br ?--><\/p>\n<pre class=\"lang:default decode:true\">&lt;?php\r\n$result = $mysqli-&gt;query(\"SELECT lastname FROM employees\");\r\n?&gt;<\/pre>\n<h3 id=\"2ConnecttoMySQL\">Method #2: Connect to MySQL using PHP Data Objects (PDO)<\/h3>\n<p>The MySQL Improved extension works only with MySQL databases. But PDO, swipes database access and allows you to create code that can manage different types of databases.<\/p>\n<p><strong>For connecting to MySQL using PDO, follow these steps:<\/strong><\/p>\n<p>Using the below PHP code connect to MySQL and select a database. Change the username, password, and dbname as per yours:<\/p>\n<pre class=\"lang:default decode:true\">&lt;?php\r\n    $myPDO = new PDO('mysql:host=localhost;dbname=dbname', 'username', 'password');\r\n?&gt;<\/pre>\n<p>Once the code connects to MySQL and selects the database, you can run SQL queries and perform other operations. For instance, the below PHP code runs a SQL query that displays the last names from the employees table, and stores the result in the $result variable:<br \/>\n<!--?php $result = $myPDO-&gt;query(\"SELECT lastname FROM employees\");&lt;br ?--><\/p>\n<pre class=\"lang:default decode:true\">&lt;?php\r\n    $result = $myPDO-&gt;query(\"SELECT lastname FROM employees\");\r\n?&gt;<\/pre>\n<h3 id=\"3ConnecttoMySQL\">Method #3: Connect to MySQL Using Legacy PHP Functions<\/h3>\n<p>The original PHP MySQL functions (beginning with mysql_) are disapproved in PHP 5.5, and will finally be removed from PHP. Therefore, only use these functions when extremely necessary for backward compatibility. Use the MySQL Improved extension or PDO instead at the most.<\/p>\n<p><strong>For connecting to MySQL using the legacy PHP MySQL functions, follow below steps:<\/strong><\/p>\n<p>Use the below PHP code for connecting to MySQL and select a database. Change the username, password, and dbname as per yours:<\/p>\n<pre class=\"lang:default decode:true\">&lt;?php\r\nmysql_connect('localhost','username','password'); mysql_select_db(\"dbname\");\r\n?&gt;<\/pre>\n<p>Once the code connects to MySQL and selects the database, you are allowed to run SQL queries and perform other operations. For instance, the below PHP code runs a SQL query that displays the last names from the employees table, and stores the result in the $result variable:<\/p>\n<pre class=\"lang:default decode:true\">&lt;?php\r\n    $result = mysql_query('SELECT lastname FROM employees');\r\n?&gt;<\/pre>\n<h3 id=\"4ConnecttoMySQL\">Method #4: Connect to Remote MySQL Databases with the use of PHP<\/h3>\n<p>In the previous examples, it is assumed that the PHP script runs on the same server where the MySQL database is located. But what is the solution if you want to use PHP to connect to a MySQL database from a remote location? For instance, you may want to connect to your MilesWeb database from a home computer or from another web server.<\/p>\n<p><strong>For this, you need to do two things:<\/strong><\/p>\n<p>1. On the MilesWeb server, enable the connecting IP address for remote access.<\/p>\n<p><strong>Note:<\/strong> If you don\u2019t add your IP address to the list of permitted remote access hosts, you will get the Access denied messages while accessing a MySQL database remotely.<\/p>\n<p>2. Go to your PHP code and change the MySQL connection string to use the MilesWeb server name instead of localhost. Replace \u201cxxxx.xxx.xxxx\u201d with localhost, if you connect using locally and replace it with the server IP, if you connect remotely:<br \/>\n<!--?php $mysqli = new mysqli(\"xxxx.milesweb.com\", \"username\", \"password\", \"dbname\"); ?--><\/p>\n<pre class=\"lang:default decode:true\">&lt;?php\r\n    $mysqli = new mysqli(\"xxxx.xxx.xxxx\", \"username\", \"password\", \"dbname\");\r\n?&gt;<\/pre>\n<p>You will now be able to connect to MySQL with the use of PHP.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this article, you will learn to connect to a MySQL database using PHP with the below methods: Table of Contents 1. Method #1: Connect to MySQL with MySQL Improved 2. Method #2: Connect to MySQL using PHP Data Objects (PDO) 3. Method #3: Connect to MySQL Using Legacy PHP Functions 4. Method #4: Connect [&hellip;]<\/p>\n","protected":false},"author":16,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[947],"tags":[1663,1676,205,18],"class_list":["post-8711","post","type-post","status-publish","format-standard","placeholder-for-hentry","category-mysql","tag-connect-to-mysql","tag-connect-to-mysql-with-use-of-php","tag-mysql","tag-php"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v25.2 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>How to Connect to MySQL with the use of PHP?<\/title>\n<meta name=\"description\" content=\"The article explains the methods to connect to MySQL with PHP.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.milesweb.co.uk\/hosting-faqs\/connect-to-mysql-with-the-use-of-php\/\" \/>\n<meta property=\"og:locale\" content=\"en_GB\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How to Connect to MySQL with the use of PHP?\" \/>\n<meta property=\"og:description\" content=\"The article explains the methods to connect to MySQL with PHP.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.milesweb.co.uk\/hosting-faqs\/connect-to-mysql-with-the-use-of-php\/\" \/>\n<meta property=\"og:site_name\" content=\"Web Hosting FAQs by MilesWeb\" \/>\n<meta property=\"article:published_time\" content=\"2020-11-30T10:46:23+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2020-11-30T10:47:18+00:00\" \/>\n<meta name=\"author\" content=\"Pallavi Godse\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Pallavi Godse\" \/>\n\t<meta name=\"twitter:label2\" content=\"Estimated reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.milesweb.co.uk\/hosting-faqs\/connect-to-mysql-with-the-use-of-php\/\",\"url\":\"https:\/\/www.milesweb.co.uk\/hosting-faqs\/connect-to-mysql-with-the-use-of-php\/\",\"name\":\"How to Connect to MySQL with the use of PHP?\",\"isPartOf\":{\"@id\":\"https:\/\/www.milesweb.co.uk\/hosting-faqs\/#website\"},\"datePublished\":\"2020-11-30T10:46:23+00:00\",\"dateModified\":\"2020-11-30T10:47:18+00:00\",\"author\":{\"@id\":\"https:\/\/www.milesweb.co.uk\/hosting-faqs\/#\/schema\/person\/7e3952607fa9eb4e82fea9f7cad9c945\"},\"description\":\"The article explains the methods to connect to MySQL with PHP.\",\"breadcrumb\":{\"@id\":\"https:\/\/www.milesweb.co.uk\/hosting-faqs\/connect-to-mysql-with-the-use-of-php\/#breadcrumb\"},\"inLanguage\":\"en-GB\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.milesweb.co.uk\/hosting-faqs\/connect-to-mysql-with-the-use-of-php\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.milesweb.co.uk\/hosting-faqs\/connect-to-mysql-with-the-use-of-php\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.milesweb.co.uk\/hosting-faqs\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"How to Connect to MySQL with the use of PHP?\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.milesweb.co.uk\/hosting-faqs\/#website\",\"url\":\"https:\/\/www.milesweb.co.uk\/hosting-faqs\/\",\"name\":\"Web Hosting FAQs by MilesWeb\",\"description\":\"\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.milesweb.co.uk\/hosting-faqs\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-GB\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.milesweb.co.uk\/hosting-faqs\/#\/schema\/person\/7e3952607fa9eb4e82fea9f7cad9c945\",\"name\":\"Pallavi Godse\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-GB\",\"@id\":\"https:\/\/www.milesweb.co.uk\/hosting-faqs\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/eefc9695ea2b2c6e143c9c9919701aaa?s=96&d=blank&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/eefc9695ea2b2c6e143c9c9919701aaa?s=96&d=blank&r=g\",\"caption\":\"Pallavi Godse\"},\"description\":\"Pallavi is a Digital Marketing Executive at MilesWeb and has an experience of over 4 years in content development. She is interested in writing engaging content on business, technology, web hosting and other topics related to information technology.\",\"url\":\"https:\/\/www.milesweb.co.uk\/hosting-faqs\/author\/pallavi\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"How to Connect to MySQL with the use of PHP?","description":"The article explains the methods to connect to MySQL with PHP.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.milesweb.co.uk\/hosting-faqs\/connect-to-mysql-with-the-use-of-php\/","og_locale":"en_GB","og_type":"article","og_title":"How to Connect to MySQL with the use of PHP?","og_description":"The article explains the methods to connect to MySQL with PHP.","og_url":"https:\/\/www.milesweb.co.uk\/hosting-faqs\/connect-to-mysql-with-the-use-of-php\/","og_site_name":"Web Hosting FAQs by MilesWeb","article_published_time":"2020-11-30T10:46:23+00:00","article_modified_time":"2020-11-30T10:47:18+00:00","author":"Pallavi Godse","twitter_misc":{"Written by":"Pallavi Godse","Estimated reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.milesweb.co.uk\/hosting-faqs\/connect-to-mysql-with-the-use-of-php\/","url":"https:\/\/www.milesweb.co.uk\/hosting-faqs\/connect-to-mysql-with-the-use-of-php\/","name":"How to Connect to MySQL with the use of PHP?","isPartOf":{"@id":"https:\/\/www.milesweb.co.uk\/hosting-faqs\/#website"},"datePublished":"2020-11-30T10:46:23+00:00","dateModified":"2020-11-30T10:47:18+00:00","author":{"@id":"https:\/\/www.milesweb.co.uk\/hosting-faqs\/#\/schema\/person\/7e3952607fa9eb4e82fea9f7cad9c945"},"description":"The article explains the methods to connect to MySQL with PHP.","breadcrumb":{"@id":"https:\/\/www.milesweb.co.uk\/hosting-faqs\/connect-to-mysql-with-the-use-of-php\/#breadcrumb"},"inLanguage":"en-GB","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.milesweb.co.uk\/hosting-faqs\/connect-to-mysql-with-the-use-of-php\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.milesweb.co.uk\/hosting-faqs\/connect-to-mysql-with-the-use-of-php\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.milesweb.co.uk\/hosting-faqs\/"},{"@type":"ListItem","position":2,"name":"How to Connect to MySQL with the use of PHP?"}]},{"@type":"WebSite","@id":"https:\/\/www.milesweb.co.uk\/hosting-faqs\/#website","url":"https:\/\/www.milesweb.co.uk\/hosting-faqs\/","name":"Web Hosting FAQs by MilesWeb","description":"","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.milesweb.co.uk\/hosting-faqs\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-GB"},{"@type":"Person","@id":"https:\/\/www.milesweb.co.uk\/hosting-faqs\/#\/schema\/person\/7e3952607fa9eb4e82fea9f7cad9c945","name":"Pallavi Godse","image":{"@type":"ImageObject","inLanguage":"en-GB","@id":"https:\/\/www.milesweb.co.uk\/hosting-faqs\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/eefc9695ea2b2c6e143c9c9919701aaa?s=96&d=blank&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/eefc9695ea2b2c6e143c9c9919701aaa?s=96&d=blank&r=g","caption":"Pallavi Godse"},"description":"Pallavi is a Digital Marketing Executive at MilesWeb and has an experience of over 4 years in content development. She is interested in writing engaging content on business, technology, web hosting and other topics related to information technology.","url":"https:\/\/www.milesweb.co.uk\/hosting-faqs\/author\/pallavi\/"}]}},"views":496,"_links":{"self":[{"href":"https:\/\/www.milesweb.co.uk\/hosting-faqs\/wp-json\/wp\/v2\/posts\/8711","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.milesweb.co.uk\/hosting-faqs\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.milesweb.co.uk\/hosting-faqs\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.milesweb.co.uk\/hosting-faqs\/wp-json\/wp\/v2\/users\/16"}],"replies":[{"embeddable":true,"href":"https:\/\/www.milesweb.co.uk\/hosting-faqs\/wp-json\/wp\/v2\/comments?post=8711"}],"version-history":[{"count":4,"href":"https:\/\/www.milesweb.co.uk\/hosting-faqs\/wp-json\/wp\/v2\/posts\/8711\/revisions"}],"predecessor-version":[{"id":8715,"href":"https:\/\/www.milesweb.co.uk\/hosting-faqs\/wp-json\/wp\/v2\/posts\/8711\/revisions\/8715"}],"wp:attachment":[{"href":"https:\/\/www.milesweb.co.uk\/hosting-faqs\/wp-json\/wp\/v2\/media?parent=8711"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.milesweb.co.uk\/hosting-faqs\/wp-json\/wp\/v2\/categories?post=8711"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.milesweb.co.uk\/hosting-faqs\/wp-json\/wp\/v2\/tags?post=8711"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}