---
title: "How to use PHP to Connect and Retrieve Data from MySQL"
description: "In our previous set of articles, we've created a simple 2 page website that allows users to submit comments about the page they were looking at. In this article, we're going to show you how to use..."
url: https://www.inmotionhosting.com/support/website/grab-all-comments-from-database/
date: 2012-02-07
modified: 2023-08-15
author: "Brad Markle"
categories: ["Website", "Working with Databases"]
type: post
lang: en
---

# How to use PHP to Connect and Retrieve Data from MySQL

In our [previous set of articles](https://www.inmotionhosting.com/support/website/dynamic-website-vs-static-website/), we’ve created a simple 2 page website that allows users to submit comments about the page they were looking at. In this article, we’re going to show you how to use PHP to **Connect** to and **Retrieve Data** from **MySQL**.

## Step 1. Create our SQL Query to grab all comments

In order to display comments on a page, we first need to know what comments to show. When we set up our site we created two pages, and each page was assigned a unique id number. This ID number will be used to gather comments for that specific page. For example, when the user is on page 1, we’ll select all of the comments in the database assigned to page “1”. To ensure seamless functionality like this, it’s essential to have reliable **[PHP hosting](https://www.inmotionhosting.com/php-hosting)** that supports efficient database interactions.

If you’re not familiar with SQL, you can use phpMyAdmin to help write your SQL command. To do this:

1. [Log into cPanel](/support/edu/cpanel/how-to-log-into-cpanel/) and click the phpMyAdmin icon
2. In the left menu, first click your database name and then click the table to work with. If you’re following our example, we’ll first click on “_mysite” and then “comments”.
3. Click “Search” in the top menu
4. Enter 1 for the “Value” of “articleid” and then click “Go”[![create-sample-select-command-using-phpmyadmin-use-search](https://www.inmotionhosting.com/support/wp-content/uploads/2012/02/website_phpmysql_display_comments_create-sample-select-command-using-phpmyadmin-use-search-1024x448.gif)](/support/wp-content/uploads/2012/02/website_phpmysql_display_comments_create-sample-select-command-using-phpmyadmin-use-search.gif)  
5. After running the search, phpMyAdmin will show you all comments that belong to article 1, as well as the SQL syntax you can use to select those comments. The code provided is: `SELECT * FROM `comments` WHERE `articleid` =1 LIMIT 0 , 30` [![our-sample-select-query-from-phpmyadmin](https://www.inmotionhosting.com/support/wp-content/uploads/2012/02/website_phpmysql_display_comments_our-sample-select-query-from-phpmyadmin.gif)](/support/wp-content/uploads/2012/02/website_phpmysql_display_comments_our-sample-select-query-from-phpmyadmin.gif)    

## Step 2. Setting up our PHP code to SELECT our comments

Note that **mysqli_fetch_array** was deprecated in PHP versions below 7.0. As of 7.0, the code has been removed and replaced with **mysqli_fetch-array**.

Now that we have our sample SQL query, we can use it to create the php code that will print all comments on a page. Below is the example code that we created. If you’re not familiar with php, any line that begins with a // is a comment, and comments are used by developers to document their code. In our example, we have quite a few comments to help explain what the code is doing, but keep in mind that most scripts do not have as many comments.

```
