Select data from MySQL

Select Data From MySQL

MySQL is the Relational Database Management System (RDBMS) which is the server side container containing all the data in form of related tables. Each table represents one entity. And these entities are interrelated.

Other piece of the server side technologies are APIs or frameworks that are used to perform all the tasks such as GET data from the server or POST data to the server. Some of these server side technologies are ASP.NET, PHP, JSP etc.

Following is an example of PHP to fetch data from the MySQL database from the server and show it on the client side in the browser.

Select Data With MySQLi

The function call that selects data from MySQL database through PHP is MySQLi. The following example selects the id, firstname and lastname columns from the MyGuests table and displays it on the page:

Example (MySQLi Object-oriented)


Code lines to explain from the example above:

First, we set up an SQL query that selects the id, firstname and lastname columns from the MyGuests table. The next line of code runs the query and puts the resulting data into a variable called $result.

Then, the function num_rows() checks if there are more than zero rows returned.

If there are more than zero rows returned, the function fetch_assoc() puts all the results into an associative array that we can loop through. The while() loop loops through the result set and outputs the data from the id, firstname and lastname columns.

  • Show Comments (0)

Your email address will not be published. Required fields are marked *

comment *

  • name *

  • email *

  • website *