Question

I have a mysql database with some tables, I did a php script that will print a table like this:

[user name]

[field 1][field 2][field 3]

[value 1][value 1][value 1]

...etc, containing "n" values from each field corresponding to the user name.

but what I want is to display this table for each user, a loop printing this table for every user in my database. I know I will have to do a loop before the tag right? but I don't know which conditions this loop will have,someone could help me showing me how to do this?

Was it helpful?

Solution

You can try something like this. Note that You'll have to add database connection information and change database and table names.

$db_name = 'database';
$table_name = 'table';
mysql_connect() or die("MySQL Error: ". mysql_error());
mysql_select_db($db_name) or die("MySQL Error: ". mysql_error());
$columns_q = mysql_query("SHOW COLUMNS FROM $table_name");
if(!$columns_q){
    die("Error fetching columns $table_name: " . mysql_error());
}
echo '<table><tr>';
while($column = mysql_fetch_assoc($columns_q)){
    echo '<td>' . $column['Field'] . '</td>';
}
echo '</tr>';
$fields_q = mysql_query("SELECT * FROM $table_name");
if(!$fields_q){
    die("Error fetching fields $table_name: " . mysql_error());
}
while($fields = mysql_fetch_assoc($fields_q)){
    echo '<tr>';
    foreach ($fields as $key => $value) {
        echo '<td>' . $value . '</td>';
    }
    echo '</tr>';
}
echo '</table>';
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top