Connecting to a Database using PHP Carrie SmahaUpdated on August 15, 2023 2 Minute Read 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 shows all the fields for a specific table you specify in the code. IMPORTANT: In order for the database connection to work, you will need to create the database, add the database user, and be sure that you attach a MySQL user to the database 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 Apache, MySQL, and PHP. You can do this by installing WAMP (Windows), MAMP (Mac), or XAMPP. How to Connect to a Database Using PHP <?php //Sample Database Connection Script //Setup connection variables, such as database username //and password $hostname="localhost"; $username="your_dbusername"; $password="your_dbpassword"; $dbname="your_dbusername"; $usertable="your_tablename"; $yourfield = "your_field"; //Connect to the database $connection = mysql_connect($hostname, $username, $password); mysql_select_db($dbname, $connection); //Setup our query $query = "SELECT * FROM $usertable"; //Run the Query $result = mysql_query($query); //If the query returned results, loop through // each result if($result) { while($row = mysql_fetch_array($result)) { $name = $row["$yourfield"]; echo "Name: " . $name; } } ?> NOTE: cPanel accounts using PHP 7 or higher would need to use mysqli instead of mysql – e.g. $connection = mysqli_connect($hostname, $username, $password); You can find your PHP version in cPanel or a phpinfo page. So let’s take a look at the actual code and what you need to replace: $hostname: This almost always refers to ‘localhost’ unless you are connecting to an external database. $username: This is the MySQL user you want to connect with. Keep in mind the user must be assigned to the database. $password: This is the password for the username you just entered. $dbname: This refers to the database name you wish to connect to. $usertable: This is not needed to connect but in this script, it refers to a specific table within the database. $yourfield: This is not needed to connect to the database but tells the script which field to echo to the screen. With reliable PHP web hosting, you can effortlessly create and maintain dynamic websites and applications that interact with databases efficiently. Share this Article Carrie Smaha Senior Manager Marketing Operations 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. More Articles by Carrie Related Articles Intro to Migrating your WordPress Site Data Migrating your WordPress Database Migrating WordPress Files Configuring WordPress After a Migration Testing your WordPress website after Migration How to Move WordPress from a Subfolder to the Root Directory What to expect during a mass server migration Move Your WordPress Site to a New Server Moving Websites Built with Older Technology into WordPress How to Export Your WordPress Sites