I have a table in MySQL DB like this one:
> NAME | VALUE
>--------------------
> Jon | 0.2
> Galson | 0.34
> Sam | 0.5
I need to display this table in my web app with something like this:
> NAME | Jon | Galson | Sam
> VALUE | 0.2 | 0.34 | 0.5
I just want to convert database column into rows using MYSQL + PHP only for display purpose.
You should not be doing this using a (probably complex) SQL query. Instead rely on HTML/CSS for this:
Assuming
$data = [{name: 'Jon', value: 0.2}, {name: 'Galson', value: 0.34}, {name: 'Sam', value: 0.5}]
you could
foreach($data as $person) {
echo '<div class="person">';
echo '<div class="name">' . $person->name . '</div>';
echo '<div class="value">' . $person->value . '</div>';
echo '</div>';
}
and
.person {
float: left;
}
Also don't, never ever, under any circumstances, build HTML like that. Use some kind of templates.
You should check out @Sergiu Paraschiv answer. What you definitely do not want to do is use the deprecated mysql_db_query()
. Alternatively try something like this:
<?php
$db = new PDO('mysql:host=localhost;dbname=testdb;charset=utf8', 'username', 'password');
$stmt = $db->query('SELECT name, value FROM table');
$results = $stmt->fetchAll(PDO::FETCH_ASSOC);
$output = [];
foreach($results as $row) {
$output[$row['name']] = $row['value'];
}
print_r($output);