# How can I display the results of mysql_fetch_assoc in a dynamic table?

I have a MySQL database with several columns and two rows of data, so far. I'm using mysql_fetch_assoc to return the data in database, but I don't know how to put it all together in a table so that I can display it in another file.

Here is the code of the file that does the process (query.php):

<?php

// Configure connection settings

$db = 'scorecard';$db_admin = 'root';
$db_password = '********';$tablename = 'scoreboard';

// Title

//echo "<b>DIV!</b>";

// Connect to DB

$sql = mysql_connect("localhost",$db_admin, $db_password) or die(mysql_error()); mysql_select_db("$db", $sql); // Fetch the data$query = "SELECT * FROM $tablename";$result = mysql_query($query); echo mysql_error(); echo "<table width='100%'>\n"; echo "<tr>\n"; // Return the results, loop through them and echo while($row = mysql_fetch_array($result, MYSQL_ASSOC)) {$codcliente = $row['codcliente'];$nombre = $row['nombre'];$ejecutivo = $row['ejecutivo'];$banca_as400 = $row['banca_as400'];$banca_real = $row['banca_real'];$ingresos = $row['ingresos'];$ciiu = $row['ciiu'];$division = $row['division'];$actividad = $row['actividad'];$riesgo_industria = $row['riesgo_industria'];$riesgo_cliente = $row['riesgo_cliente'];$fecha = $row['fecha'];$analista = $row['analista']; echo "<tr><td>$codcliente</td><td>$nombre</td><td>$ejecutivo</td><td>$banca_as400</td><td>$banca_real</td><td>$ingresos</td><td>$ciiu</td><td>$division</td><td>$actividad</td><td>$riesgo_industria</td><td>$riesgo_cliente</td><td>$fecha</td><td>$analista</td></tr>\n";
echo "<tr><td>$codcliente</td><td>$nombre</td><td>$ejecutivo</td><td>$banca_as400</td><td>$banca_real</td><td>$ingresos</td><td>$ciiu</td><td>$division</td><td>$actividad</td><td>$riesgo_industria</td><td>$riesgo_cliente</td><td>$fecha</td><td>$analista</td></tr>\n"; echo "</table>\n"; }  And this is the result from the page that displays the process (index.php): CAN'T DISPLAY SINCE I DON'T HAVE ENOUGH REPUTATION POINTS :( As you can see, I have a table already in place and I would like the results to fall below as additional rows, but they come out all squashed together. I've tried several mysql_fetch types, but nothing works. The query results are displayed in a JavaScript DIV that is automatically refreshed by an AJAX script from another file (ajax.js). If you need to see the code for index.php or ajax.js, I could provide it. Thanks! ## 1 Answer Aren't you forgetting something, called <td>? Try <?php // Configure connection settings$db = 'scorecard';
$db_admin = 'root';$db_password = '********';
$tablename = 'scoreboard'; // Title //echo "<b>DIV!</b>"; // Connect to DB$sql = mysql_connect("localhost", $db_admin,$db_password)
or die(mysql_error());

mysql_select_db("$db",$sql);

// Fetch the data

$query = "SELECT * FROM$tablename";
$result = mysql_query($query);
echo mysql_error();

echo "<table width='100%'>\n";

// Return the results, loop through them and echo

while($row = mysql_fetch_assoc($result))
{
echo '<tr>'."\n";
echo "<td>{$row['codcliente']}</td>\n" . "<td>{$row['nombre']}</td>\n" . "<td>{$row['ejecutivo']}</td>\n" . "<td>{$row['banca_as400']}</td>\n" . "<td>{$row['banca_real']}</td>\n" . "<td>{$row['ingresos']}</td>\n" . "<td>{$row['ciiu']}</td>\n" . "<td>{$row['division']}</td>\n" . "<td>{$row['actividad']}</td>\n" . "<td>{$row['riesgo_industria']}</td>\n" . "<td>{$row['riesgo_cliente']}</td>\n" . "<td>{$row['fecha']}</td>\n" . "<td>{\$row['analista']}</td>\n";

echo '</tr>'."\n";
}

echo "</table>\n";

?>

• Hi @maz...I actually didn't forget, I posted a fork on my original code, sorry. I edited the question to reflect what I had before, since I can't paste it in the comment box. Jul 12 '11 at 12:44
• it works! All I have to do now is get it to align it with the headers in the index.php table. Thanks! P.S. Can you tell me what I was doing wrong? (look at updated code) Jul 12 '11 at 12:53
• You should look up how to format tables, I put a td around each cell, and a tr around each row, whereas you just had a tr around the entire thing. w3schools.com/html/html_tables.asp
– Ben
Jul 12 '11 at 19:11
• Thanks @maz, I had tried several ways to generate the table, but I had never used the ."\n" in the rows or the \n" . in the cells. Don't know if that made a difference. When I did it my way, it would return the first row in the DB formatted and the second squashed together. Jul 12 '11 at 19:52
• That is just so the code formatted more clearly (when you view the source), and isn't needed at all, the big difference were the <td>s surrounding each field, and the <tr> inside the loop.
– Ben
Jul 12 '11 at 20:01