...

View Full Version : column name and count



zodehala
01-16-2012, 04:46 PM
this is my codes

suppose that i do not know how many column there are in cdr table


$result = mysql_query("select*from cdr");
if (!$result) {
echo 'Could not run query: ' . mysql_error();
exit;
}

while($row = mysql_fetch_row($result)){
echo $row[0]." - ".$row[1]." - ".$row[2]." - ".$row[3]." - ".$row[4]." - ".$row[5]." - ".$row[6]."<br>";
}


how can i count column name and its count ?

BluePanther
01-16-2012, 04:53 PM
this is my codes

suppose that i do not know how many column there are in cdr table


$result = mysql_query("select*from cdr");
if (!$result) {
echo 'Could not run query: ' . mysql_error();
exit;
}

while($row = mysql_fetch_row($result)){
echo $row[0]." - ".$row[1]." - ".$row[2]." - ".$row[3]." - ".$row[4]." - ".$row[5]." - ".$row[6]."<br>";
}


how can i count column name and its count ?
Doing a count() of $row either inside or just after the while loop will give you the total number of columns. Is this what you're looking for?

sunfighter
01-16-2012, 04:56 PM
This is my code. Adapt it to your circumstances.

<?php
require ('./inc/DB_connect.php');

$TABLENAME = "clan_members";
$query = "SELECT * FROM $TABLENAME";
$result = mysql_query($query);
$colCount = mysql_num_fields($result);

echo "<br /><TABLE BORDER=1>clan_members<br />";

echo "<tr>";
for($colNuber = 0; $colNuber < $colCount; $colNuber++)
{
$fieldName = mysql_field_name($result, $colNuber);
echo "<th>$fieldName</th>";
}
echo "</tr>";

while($row = mysql_fetch_row($result))
{
echo "<tr align=left VALIGN=TOP>";
for($colNumber = 0; $colNumber < $colCount; $colNumber++)
echo "<td>$row[$colNumber]</td>";
echo "</tr>";
}
echo "</table><br />";
echo '<br />---------------------------<br />';
?>



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum