Skip to content

Instantly share code, notes, and snippets.

@sanjaybhowmick
Created September 9, 2015 08:44
Show Gist options
  • Save sanjaybhowmick/82cec8b3500672e47502 to your computer and use it in GitHub Desktop.
Save sanjaybhowmick/82cec8b3500672e47502 to your computer and use it in GitHub Desktop.
Display data horizontally from MySQL table
DROP TABLE IF EXISTS `member_table`;
CREATE TABLE `member_table` (
`id` tinyint(4) NOT NULL AUTO_INCREMENT,
`member_name` varchar(20) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=latin1;
-- ----------------------------
-- Records of member_table
-- ----------------------------
INSERT INTO `member_table` VALUES ('1', 'Sir Isaac Newton');
INSERT INTO `member_table` VALUES ('2', 'Leonardo Vinchi');
INSERT INTO `member_table` VALUES ('3', 'William Shakespeare');
INSERT INTO `member_table` VALUES ('4', 'Adolf Hitler');
INSERT INTO `member_table` VALUES ('5', 'Siddhartha Gautama');
INSERT INTO `member_table` VALUES ('6', 'Moses');
INSERT INTO `member_table` VALUES ('7', 'Abraham');
INSERT INTO `member_table` VALUES ('8', 'Jesus of Nazareth');
INSERT INTO `member_table` VALUES ('9', 'Walt Disney');
<?php
mysql_connect ('localhost','root','password');
mysql_select_db ('test');
?>
<table>
<tr>
<?php
$sql_result = mysql_query ("SELECT member_name FROM member_table") or die ('Unable to select'. mysql_error());
$record_count = 0; //Keeps count of the records echoed.
while ($row=mysql_fetch_row($sql_result))
{
//Check to see if it is time to start a new row
//Note: the first time through when
//$record_count==0, don't start a new row
if ($record_count % 3==0 && $record_count != 0)
{
echo '</tr><tr>';
}
echo '<td>';
//Echo out the entire record in one table cell:
for ($i=0; $i< count($row); $i++)
{
echo $row[$i];
}
echo '</td>';
//Indicate another record has been echoed:
$record_count++;
}
?>
</tr>
</table>
@tanmoyc96
Copy link

Thank You so much

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment