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 PHP to Connect to and Retrieve Data from MySQL. Show Step 1. Create our SQL Query to grab all commentsIn order to display comments on a page, we first need to know what comments to show. When we setup 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”. If you’re not familiar with SQL, you can use phpMyAdmin to help write your SQL command. To do this:
Step 2. Setting up our PHP code to SELECT our commentsNote 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.
As stated earlier, we purposely include many comments to help explain what the code was doing. While the example code above looks like a lot of work, if we strip out all of the comments, the code looks more like:
Step 3. Placing our php code into our pagesWe now have our php code that will display comments to the screen. In a previous article, we explained how to use php’s include function to reuse code, and we will continue to use this method to use our php code. To incorporate our php code:
After performing the steps above, our page1.php file now looks like this:
After testing our two pages, you can see that each page shows only the comments that were added to that particular page: How to retrieve data from SQL database?In SQL, to retrieve data stored in our tables, we use the SELECT statement. The result of this statement is always in the form of a table that we can view with our database client software or use with programming languages to build dynamic web pages or desktop applications.
How to access data in MySQL?ACCESS MYSQL DATABASE. Log into your Linux web server via Secure Shell.. Open the MySQL client program on the server in the /usr/bin directory.. Type in the following syntax to access your database: $ mysql -h {hostname} -u username -p {databasename} Password: {your password}. How to retrieve a column from MySQL database?SELECT what_to_select FROM which_table WHERE conditions_to_satisfy; what_to_select indicates what you want to see. This can be a list of columns, or * to indicate “all columns.” which_table indicates the table from which you want to retrieve data. The WHERE clause is optional.
How to get all data from table in MySQL?The first command you will need to use is the SELECT FROM MySQL statement that has the following syntax: SELECT * FROM table_name; This is a basic MySQL query which will tell the script to select all the records from the table_name table.
|