0
\$\begingroup\$

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!

asked Jul 12, 2011 at 4:19
\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

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";
?>
answered Jul 12, 2011 at 6:37
\$\endgroup\$
5
  • \$\begingroup\$ 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. \$\endgroup\$ Commented Jul 12, 2011 at 12:44
  • \$\begingroup\$ 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) \$\endgroup\$ Commented Jul 12, 2011 at 12:53
  • \$\begingroup\$ 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 \$\endgroup\$ Commented Jul 12, 2011 at 19:11
  • \$\begingroup\$ 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. \$\endgroup\$ Commented Jul 12, 2011 at 19:52
  • \$\begingroup\$ 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. \$\endgroup\$ Commented Jul 12, 2011 at 20:01

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.