Display data horizontally from MySQL table

Generally we fetch data from MySQL table and display the fetch records in rows one by one. But sometimes we need to display the records horizontally as per the image below.

Sir Isaac Newton Leonardo Vinchi William Shakespeare
Adolf Hitler Siddhartha Gautama Ian McKellen
Abraham Jesus of Nazareth Walt Disney

It’s not a tough job. You can see that the records are pulling up and displaying 2 records in a row and after that in the next rows for the next 2 records. The code for such display style is as follows:

You may also like

5 Comments

  1. This is the simplest code to select and display records from MySQL database table and display in PHP.

    $cn=mysql_connect($db_host,$db_user,$db_password) or die(mysql_error());
    mysql_select_db($db_name,$cn) or die(mysql_error());

    $sql = “SELECT field_name FROM table_name”;
    $rs = mysql_query($sql) or die(mysql_error());

    while($row = mysql_fetch_array($rs)){

    echo $field_name = $row[“field_name”];
    echo “”;

    }
    mysql_free_result($rs);

    Source:
    http://phphelp.co/2012/04/26/how-to-select-and-display-mysql-records-data-in-php/

    OR

    http://addr.pk/acfd

    1. @Aneeq – your query will display all the records without separating the record set in row wise. In my example you can see that the fetched records are being displayed in row with 2 columns and then goes to next row. Hope this makes sense.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.