I’m working in php/mysql. I’m a starter and I have a problem.
I have connected with my database and I want to create a table from the database’s table:
The first one is a table in my database with all users’ data. (they are all numerical)
- - - - - - - - - - - - - - - - - - - - - -
| id | age | score | bonus |
- - - - - - - - - - - - - - - - - - - - - -
| 1 | 17 | 39 | 10 |
- - - - - - - - - - - - - - - - - - - - - -
| 2 | 12 | 31 | 10 |
- - - - - - - - - - - - - - - - - - - - - -
| 3 | 15 | 35 | 20 |
- - - - - - - -- - - - - - - - - - - - - - -
| 4 | 11 | 32 | 30 |
- - - - - - - - - - - - - - - - - - - - - - -
| 5 | 12 | 49 | 10 |
- - - - - - - - - - - - - - - - - - - - - - - -
etc…………………………………………………………
I want to create a table ‘Ages’ using the first two columns like that:
--- - - - - - - - - -
| id | age |
| 1 | 17 |
| 1 | 12 |
| 1 | 15 |
| 1 | 11 |
| 5 | 12 |
- - - - - - -- - - -
How could I do that?
Table from columns
Moderator: General Moderators
Re: Table from columns
Code: Select all
<?php
$sql = new PDO('mysql:host=localhost;dbname=whatever', 'username', 'password');
$query = "SELECT id, name FROM table_name";
$result = $sql->query($query)->fetchAll(PDO::FETCH_OBJ);
?>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8" />
<title>Debug</title>
</head>
<body>
<table>
<tr>
<th>ID</th>
<th>Age</th>
</tr>
<?php foreach ($result as $user): ?>
<tr>
<td><?php echo $user->id; ?></td>
<td><?php echo $user->age; ?></td>
</tr>
<?php endforeach; ?>
</table>
</body>
</html>Re: Table from columns
Thank you. It was very helpful.