<?php
mysql_connect("localhost","username","password") or die ("Cannot connect" . mysql_error());
$dbname=$_GET;
$tablname=$_GET;
?>
<div id="stylized" class="myform">
<?php
echo "<span class='db'>Table:" . $tablname . "</span>";
?>
<?php
mysql_select_db($dbname);
$q=mysql_query("SELECT * FROM $tablname");
if(!$q){
die("Query Failed!");
}
$fields_num=mysql_num_fields($q);
echo "<table><tr>";
for($j=0;j<$fields_num;$j++){
$fields=mysql_fetch_field($q);
echo "<th>{$field->name}</th>";
}
echo "</tr>\n";

while($row=mysql_fetch_array($q)){
echo "<tr>";
foreach($row as $cell){
echo "<td>$cell</td>";
echo "</tr>\n";
}
}
mysql_free_result($q);
?>

in order to print the field name or to use them anywhere else u have to use the following syntax:

$result = mysql_query("select * from table"); 

for ($i = 0; $i < mysql_num_fields($result); $i++) { 
    print "<th>".mysql_field_name($result, $i)."</th>\n"; 
}

When I browse my file with having the following code to return my table data, then I am getting non-stoppable warnings. Any one can help me?

<?php
mysql_select_db($dbname);
$q=mysql_query("SELECT * FROM $tablname");
if(!$q){
die("Query Failed!");
}
echo "<table><tr>";
for($i=0;i< mysql_num_fields($q);$i++){
echo "<th>" . mysql_field_name($q) . "</th>\n";
}
while($row=mysql_fetch_array($q)){
echo "<tr>";
echo "<td>" . $row['fieldname'] . "</td>";
echo "</tr>\n";
}
?>

I solved the above problem but it only retrieves the fields, but now I need to retrieve all existing data in each column.
Any one can help me please?

<?php
mysql_select_db($dbname);
$q=mysql_query("SELECT * FROM $tablname");
if(!$q){
die("Query Failed!");
}
else {
echo "<table>";
for($i=0; $i < mysql_num_fields($q);$i++){
echo "<th>" . mysql_field_name($q,$i) . "</th>";
while($row=mysql_fetch_array($q)){
echo "<tr>";
echo "<td>" . $row['fieldname] . "</td>";
echo "</tr>";
}
}
}
echo "</table>";
?>

Hope this is helpful, the problem was with nested loop. that does not seem to be logical because we need to have the header only once.

<?php
mysql_select_db($dbname);
$q=mysql_query("SELECT * FROM $tablname");

if(!$q){
	die("Query Failed!");
}
else {
	echo "<table><tr>";
	
	for($i = 0; $i < mysql_num_fields($q); $i++){
		echo "<th>" . mysql_field_name($q,$i) . "</th>";
	}
	
	echo "</tr>";
	
	while($row = mysql_fetch_array($q)){
		echo "<tr>";
			echo "<td>" . $row['fieldname'] . "</td>";
		echo "</tr>";
	}
echo "</table>";
}
?>

Maybe u can try this one

<?php
mysql_select_db($dbname);
$q=mysql_query("SELECT * FROM $tablname");

if(!$q){
	die("Query Failed!");
}
else {
	echo "<table><tr>";
	$i=0; 
    while ($i < mysql_num_fields($result)) { 
		echo "<th>" . mysql_field_name($q, $i) . "</th>";
        $fields[]=mysql_fetch_field($q, $i); 
        $i++; 
    }
	
	echo "</tr>";
	
	while($row = mysql_fetch_array($q)){
		echo "<tr>";
		for($i = 0; $i < count($row); $i++) { 
            echo "<td>" . $row[$fields[$i]] . "</td>"; 
        } 
		echo "</tr>";
	}
echo "</table>";
}
?>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.