In our previous post, we successfully created a table and inserted five records in it. Now we will display the same data in a web page through a PHP file. There are some functions used to do this and this is an important part to be known. And these functions which we are going to use will be common in all php files where you intend to display something on your web page from the database.
We created this table shown below-

and now, from the above table, if we want to display the name of all books in a web page, then following piece of code will be used.

For the above code I am using PHP designer+WAMP server. Let me now explain the code.
- ‘
mysqli_connect(hostname, username, password, database name, port, socket)‘ function opens a new connection to the MySQL server. It has six optional parameters. I have used only first four of these. ‘myproject’ is the name of my own project you write yours and there is no password for my database so I have left it blank “”. - In line 7 I have stored my query in a variable $query. Please note that we pass queries in database, we cannot pass the queries elsewhere in the same manner as we do in database and since this is a PHP file, we are storing the query as a string in variable $query.
- In line 9, the function mysqli_query() function performs the query (that we stored in $query) in the database(we have sought the connection and stored it in $link) and so we have passed $link and $query inside it. This returns an object. You can view its return type through
var_dump(mysqli_query($link, $query)); - In line 11
mysqli_fetch_row()function returns a row from the $resultset. Please note that $resultset variable holds all the records of the table but it is not in the proper format to be displayed. That’s why we use this function- to actually pick data from the table and display it in the way/format we want. - And the row extracted from the table through
mysqli_fetch_rowis stored in $r. That means $r is now an array. And so we can display whatever data we want with the use of indices. - I have displayed $r[0], because in the table the name of book is stored in first column of each row that is at index 0 of each row. I hope you got this explanation and if not then no issues simply write your doubt in comments.
With this you can display the name of books stored in your database in a web page. But remember this is not at all a proper way to do. We must have seen in various sites that data is presented in a proper way mostly in table form.
We’ll also display this data in a proper table format in our next post. Till then keep coding and keep consuming coffee cups :).
Have ERROR FREE coding!
