Click to See Complete Forum and Search --> : displaying an entire Mysql table using php


Andrewpca
11-15-2005, 08:50 AM
Does anyone know how to display a whole mysql table for a client when they log into my website.

www.p-c-a.co.uk
(for what help it is)

but tables of statistical info need to be available for clients.
Some restrictions may be necessary but adding user priveleges to their login info is easy.

Does anyone have an example of some code?

Thanks.

Andy

bokeh
11-15-2005, 09:46 AM
Hmm! Display absolutely everything? You could do it like this:<?php

$mysql = mysql_connect('localhost','root','');
mysql_select_db('dbname'); // fill in
$tablename = 'companies'; // fill in

$result = mysql_query("DESC `$tablename`");
while($row = mysql_fetch_assoc($result)) $fields[] = $row['Field'];
echo '<table style="margin: 0 25px 0 25px" border="1" cellspacing="0" cellpadding="6"><tr>';
foreach($fields as $field) echo '<th nowrap="nowrap">'.$field.'</th>';
echo '</tr>';
$result = mysql_query("SELECT * FROM `$tablename`");
while($row = mysql_fetch_array($result)){
extract($row);
echo '<tr>';
foreach($fields as $field){
if(!empty($row[$field])){
echo '<td>'.$row[$field].'</td>';
}else{
echo '<td>&nbsp;</td>';
}
}
echo '</tr>';
}
echo '</table>';
?>