{"id":340,"date":"2011-12-13T20:28:04","date_gmt":"2011-12-13T20:28:04","guid":{"rendered":"https:\/\/www.inmotionhosting.com\/support\/2011\/12\/13\/connecting-to-a-database-using-php\/"},"modified":"2023-08-15T16:48:49","modified_gmt":"2023-08-15T20:48:49","slug":"connecting-to-a-database-using-php","status":"publish","type":"post","link":"https:\/\/www.inmotionhosting.com\/support\/website\/connecting-to-a-database-using-php\/","title":{"rendered":"Connecting to a Database using PHP"},"content":{"rendered":"<p>Selecting a reputable <a href=\"https:\/\/www.inmotionhosting.com\/\">web hosting company<\/a> is only the first step towards building and maintaining a successful website. Sometimes you may need to <strong>connect your PHP driven website to a database<\/strong>. For most content management systems, this is done through the config.php file. Below is a sample PHP script that connects to a database and shows all the fields for a specific table you specify in the code.<\/p>\n<div class=\"alert alert-warning\"><b>IMPORTANT:<\/b> In order for the database connection to work, you will need to <a href=\"\/support\/\" target=\"_blank\" rel=\"noopener noreferrer\">create the database<\/a>, <a href=\"\/support\/edu\/cpanel\/create-database-user\/\" target=\"_blank\" rel=\"noopener noreferrer\">add the database user<\/a>, and be sure that you <a href=\"\/support\/edu\/cpanel\/create-database-user\/#assign-user-to-db\">attach a MySQL user to the database<\/a> before attempting to run the script on the server.If you need to run a database script on your local computer, you will need to set up your computer to run <strong>Apache<\/strong>, <strong>MySQL<\/strong>, and <strong>PHP<\/strong>. You can do this by installing <a href=\"https:\/\/www.wampserver.com\/en\/\" target=\"_blank\" rel=\"noopener noreferrer\">WAMP<\/a> (Windows), <a href=\"https:\/\/www.mamp.info\/en\/index.html\" target=\"_blank\" rel=\"noopener noreferrer\">MAMP<\/a> (Mac), or <a href=\"https:\/\/www.apachefriends.org\/en\/xampp.html\" target=\"_blank\" rel=\"noopener noreferrer\">XAMPP<\/a>.<\/p>\n<\/div>\n<h2>How to Connect to a Database Using PHP<\/h2>\n<pre class=\"code_block\" style=\"margin-let: 20px;\">&lt;?php   \n\n\/\/Sample Database Connection Script    \n\/\/Setup connection variables, such as database username \n\/\/and password \n\n$hostname=\"localhost\"; \n$username=\"your_dbusername\"; \n$password=\"your_dbpassword\"; \n$dbname=\"your_dbusername\"; \n$usertable=\"your_tablename\"; \n$yourfield = \"your_field\";   \n\n\/\/Connect to the database \n$connection = mysql_connect($hostname, $username, $password); mysql_select_db($dbname, $connection);   \n\n\/\/Setup our query \n$query = \"SELECT * FROM $usertable\";   \n\n\/\/Run the Query \n$result = mysql_query($query);   \n\n\/\/If the query returned results, loop through \n\/\/ each result \n\nif($result) \n    {   while($row = mysql_fetch_array($result))   \n        {     $name = $row[\"$yourfield\"];     \n              echo \"Name: \" . $name; \n   } }   \n\n?&gt;\n\n<\/pre>\n<div class=\"alert alert-info\">\n<p><b>NOTE:<\/b> cPanel accounts using PHP 7 or higher would need to use <em>mysqli<\/em> instead of <em>mysql<\/em> &#8211;<br \/>\ne.g. <em>$connection = mysqli_connect($hostname, $username, $password);<\/em><\/p>\n<p>You can <a href=\"\/support\/website\/how-to-change-the-php-version-your-account-uses\/\" target=\"_blank\" rel=\"noopener noreferrer\">find your PHP version in cPanel<\/a> or a <a href=\"\/support\/website\/find-php-version-and-options-of-your-server\/\" target=\"_blank\" rel=\"noopener noreferrer\">phpinfo page<\/a>.<\/p>\n<\/div>\n<p>So let&#8217;s take a look at the actual code and what you need to replace:<\/p>\n<p><strong>$hostname<\/strong>: This almost always refers to &#8216;localhost&#8217; unless you are connecting to an external database.<\/p>\n<p><strong>$username<\/strong>: This is the MySQL user you want to connect with. Keep in mind the user must be assigned to the database.<\/p>\n<p><strong>$password<\/strong>: This is the password for the username you just entered.<\/p>\n<p><strong>$dbname<\/strong>: This refers to the database name you wish to connect to.<\/p>\n<p><strong>$usertable<\/strong>: This is not needed to connect but in this script, it refers to a specific table within the database.<\/p>\n<p><strong>$yourfield<\/strong>: This is not needed to connect to the database but tells the script which field to echo to the screen.<\/p>\n<p>With reliable <a href=\"https:\/\/www.inmotionhosting.com\/php-hosting\">PHP web hosting<\/a>, you can effortlessly create and maintain dynamic websites and applications that interact with databases efficiently.<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Selecting a reputable web hosting company is only the first step towards building and maintaining a successful website. Sometimes you may need to connect your PHP driven website to a database. For most content management systems, this is done through the config.php file. Below is a sample PHP script that connects to a database and<a class=\"moretag\" href=\"https:\/\/www.inmotionhosting.com\/support\/website\/connecting-to-a-database-using-php\/\"> Read More ><\/a><\/p>\n","protected":false},"author":56983,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[4288,4305],"tags":[],"class_list":["post-340","post","type-post","status-publish","format-standard","hentry","category-website","category-databases"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.1.1 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Connecting to a Database using PHP | InMotion Hosting<\/title>\n<meta name=\"description\" content=\"Learn how to connect to a MySQL database using 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.inmotionhosting.com\/support\/website\/connecting-to-a-database-using-php\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Connecting to a Database using PHP | InMotion Hosting\" \/>\n<meta property=\"og:description\" content=\"Learn how to connect to a MySQL database using PHP.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.inmotionhosting.com\/support\/website\/connecting-to-a-database-using-php\/\" \/>\n<meta property=\"og:site_name\" content=\"InMotion Hosting Support Center\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/inmotionhosting\/\" \/>\n<meta property=\"article:published_time\" content=\"2011-12-13T20:28:04+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-08-15T20:48:49+00:00\" \/>\n<meta name=\"author\" content=\"Carrie Smaha\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@carriesmaha\" \/>\n<meta name=\"twitter:site\" content=\"@InMotionHosting\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Carrie Smaha\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/www.inmotionhosting.com\/support\/website\/connecting-to-a-database-using-php\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/www.inmotionhosting.com\/support\/website\/connecting-to-a-database-using-php\/\"},\"author\":{\"name\":\"Carrie Smaha\",\"@id\":\"https:\/\/www.inmotionhosting.com\/support\/#\/schema\/person\/595948dab2995d347a87076abdae19d8\"},\"headline\":\"Connecting to a Database using PHP\",\"datePublished\":\"2011-12-13T20:28:04+00:00\",\"dateModified\":\"2023-08-15T20:48:49+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/www.inmotionhosting.com\/support\/website\/connecting-to-a-database-using-php\/\"},\"wordCount\":336,\"commentCount\":32,\"publisher\":{\"@id\":\"https:\/\/www.inmotionhosting.com\/support\/#organization\"},\"articleSection\":[\"Website\",\"Working with Databases\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/www.inmotionhosting.com\/support\/website\/connecting-to-a-database-using-php\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.inmotionhosting.com\/support\/website\/connecting-to-a-database-using-php\/\",\"url\":\"https:\/\/www.inmotionhosting.com\/support\/website\/connecting-to-a-database-using-php\/\",\"name\":\"Connecting to a Database using PHP | InMotion Hosting\",\"isPartOf\":{\"@id\":\"https:\/\/www.inmotionhosting.com\/support\/#website\"},\"datePublished\":\"2011-12-13T20:28:04+00:00\",\"dateModified\":\"2023-08-15T20:48:49+00:00\",\"description\":\"Learn how to connect to a MySQL database using PHP.\",\"breadcrumb\":{\"@id\":\"https:\/\/www.inmotionhosting.com\/support\/website\/connecting-to-a-database-using-php\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.inmotionhosting.com\/support\/website\/connecting-to-a-database-using-php\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.inmotionhosting.com\/support\/website\/connecting-to-a-database-using-php\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.inmotionhosting.com\/support\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Connecting to a Database using PHP\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.inmotionhosting.com\/support\/#website\",\"url\":\"https:\/\/www.inmotionhosting.com\/support\/\",\"name\":\"InMotion Hosting Support Center\",\"description\":\"Web Hosting Support &amp; Tutorials\",\"publisher\":{\"@id\":\"https:\/\/www.inmotionhosting.com\/support\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.inmotionhosting.com\/support\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/www.inmotionhosting.com\/support\/#organization\",\"name\":\"InMotion Hosting\",\"url\":\"https:\/\/www.inmotionhosting.com\/support\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.inmotionhosting.com\/support\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/www.inmotionhosting.com\/support\/wp-content\/uploads\/2023\/02\/inmotion-hosting-logo-yoast.jpg\",\"contentUrl\":\"https:\/\/www.inmotionhosting.com\/support\/wp-content\/uploads\/2023\/02\/inmotion-hosting-logo-yoast.jpg\",\"width\":696,\"height\":696,\"caption\":\"InMotion Hosting\"},\"image\":{\"@id\":\"https:\/\/www.inmotionhosting.com\/support\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/inmotionhosting\/\",\"https:\/\/x.com\/InMotionHosting\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.inmotionhosting.com\/support\/#\/schema\/person\/595948dab2995d347a87076abdae19d8\",\"name\":\"Carrie Smaha\",\"description\":\"Carrie Smaha is a Senior Marketing Operations leader with over 20 years of experience in digital strategy, web development, and IT project management. She specializes in go-to-market programs and SaaS solutions for WordPress and VPS Hosting, working closely with technical teams and customers to deliver high-performance, scalable platforms. At InMotion Hosting, she drives product marketing initiatives that blend strategic insight with technical depth.\",\"sameAs\":[\"https:\/\/www.linkedin.com\/in\/carriesmaha\/\",\"https:\/\/x.com\/carriesmaha\"],\"url\":\"https:\/\/www.inmotionhosting.com\/support\/author\/carries\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Connecting to a Database using PHP | InMotion Hosting","description":"Learn how to connect to a MySQL database using 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.inmotionhosting.com\/support\/website\/connecting-to-a-database-using-php\/","og_locale":"en_US","og_type":"article","og_title":"Connecting to a Database using PHP | InMotion Hosting","og_description":"Learn how to connect to a MySQL database using PHP.","og_url":"https:\/\/www.inmotionhosting.com\/support\/website\/connecting-to-a-database-using-php\/","og_site_name":"InMotion Hosting Support Center","article_publisher":"https:\/\/www.facebook.com\/inmotionhosting\/","article_published_time":"2011-12-13T20:28:04+00:00","article_modified_time":"2023-08-15T20:48:49+00:00","author":"Carrie Smaha","twitter_card":"summary_large_image","twitter_creator":"@carriesmaha","twitter_site":"@InMotionHosting","twitter_misc":{"Written by":"Carrie Smaha","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.inmotionhosting.com\/support\/website\/connecting-to-a-database-using-php\/#article","isPartOf":{"@id":"https:\/\/www.inmotionhosting.com\/support\/website\/connecting-to-a-database-using-php\/"},"author":{"name":"Carrie Smaha","@id":"https:\/\/www.inmotionhosting.com\/support\/#\/schema\/person\/595948dab2995d347a87076abdae19d8"},"headline":"Connecting to a Database using PHP","datePublished":"2011-12-13T20:28:04+00:00","dateModified":"2023-08-15T20:48:49+00:00","mainEntityOfPage":{"@id":"https:\/\/www.inmotionhosting.com\/support\/website\/connecting-to-a-database-using-php\/"},"wordCount":336,"commentCount":32,"publisher":{"@id":"https:\/\/www.inmotionhosting.com\/support\/#organization"},"articleSection":["Website","Working with Databases"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.inmotionhosting.com\/support\/website\/connecting-to-a-database-using-php\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.inmotionhosting.com\/support\/website\/connecting-to-a-database-using-php\/","url":"https:\/\/www.inmotionhosting.com\/support\/website\/connecting-to-a-database-using-php\/","name":"Connecting to a Database using PHP | InMotion Hosting","isPartOf":{"@id":"https:\/\/www.inmotionhosting.com\/support\/#website"},"datePublished":"2011-12-13T20:28:04+00:00","dateModified":"2023-08-15T20:48:49+00:00","description":"Learn how to connect to a MySQL database using PHP.","breadcrumb":{"@id":"https:\/\/www.inmotionhosting.com\/support\/website\/connecting-to-a-database-using-php\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.inmotionhosting.com\/support\/website\/connecting-to-a-database-using-php\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.inmotionhosting.com\/support\/website\/connecting-to-a-database-using-php\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.inmotionhosting.com\/support\/"},{"@type":"ListItem","position":2,"name":"Connecting to a Database using PHP"}]},{"@type":"WebSite","@id":"https:\/\/www.inmotionhosting.com\/support\/#website","url":"https:\/\/www.inmotionhosting.com\/support\/","name":"InMotion Hosting Support Center","description":"Web Hosting Support &amp; Tutorials","publisher":{"@id":"https:\/\/www.inmotionhosting.com\/support\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.inmotionhosting.com\/support\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.inmotionhosting.com\/support\/#organization","name":"InMotion Hosting","url":"https:\/\/www.inmotionhosting.com\/support\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.inmotionhosting.com\/support\/#\/schema\/logo\/image\/","url":"https:\/\/www.inmotionhosting.com\/support\/wp-content\/uploads\/2023\/02\/inmotion-hosting-logo-yoast.jpg","contentUrl":"https:\/\/www.inmotionhosting.com\/support\/wp-content\/uploads\/2023\/02\/inmotion-hosting-logo-yoast.jpg","width":696,"height":696,"caption":"InMotion Hosting"},"image":{"@id":"https:\/\/www.inmotionhosting.com\/support\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/inmotionhosting\/","https:\/\/x.com\/InMotionHosting"]},{"@type":"Person","@id":"https:\/\/www.inmotionhosting.com\/support\/#\/schema\/person\/595948dab2995d347a87076abdae19d8","name":"Carrie Smaha","description":"Carrie Smaha is a Senior Marketing Operations leader with over 20 years of experience in digital strategy, web development, and IT project management. She specializes in go-to-market programs and SaaS solutions for WordPress and VPS Hosting, working closely with technical teams and customers to deliver high-performance, scalable platforms. At InMotion Hosting, she drives product marketing initiatives that blend strategic insight with technical depth.","sameAs":["https:\/\/www.linkedin.com\/in\/carriesmaha\/","https:\/\/x.com\/carriesmaha"],"url":"https:\/\/www.inmotionhosting.com\/support\/author\/carries\/"}]}},"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"primary_category":null,"_links":{"self":[{"href":"https:\/\/www.inmotionhosting.com\/support\/wp-json\/wp\/v2\/posts\/340","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.inmotionhosting.com\/support\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.inmotionhosting.com\/support\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.inmotionhosting.com\/support\/wp-json\/wp\/v2\/users\/56983"}],"replies":[{"embeddable":true,"href":"https:\/\/www.inmotionhosting.com\/support\/wp-json\/wp\/v2\/comments?post=340"}],"version-history":[{"count":9,"href":"https:\/\/www.inmotionhosting.com\/support\/wp-json\/wp\/v2\/posts\/340\/revisions"}],"predecessor-version":[{"id":106029,"href":"https:\/\/www.inmotionhosting.com\/support\/wp-json\/wp\/v2\/posts\/340\/revisions\/106029"}],"wp:attachment":[{"href":"https:\/\/www.inmotionhosting.com\/support\/wp-json\/wp\/v2\/media?parent=340"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.inmotionhosting.com\/support\/wp-json\/wp\/v2\/categories?post=340"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.inmotionhosting.com\/support\/wp-json\/wp\/v2\/tags?post=340"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}