Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    Regular Coder
    Join Date
    Dec 2007
    Posts
    269
    Thanks
    28
    Thanked 0 Times in 0 Posts

    column name and count

    this is my codes

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

    PHP Code:
    $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 ?

  • #2
    Senior Coder
    Join Date
    Jul 2011
    Posts
    1,226
    Thanks
    3
    Thanked 171 Times in 171 Posts
    Quote Originally Posted by zodehala View Post
    this is my codes

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

    PHP Code:
    $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?
    Useful function to retrieve difference in times
    The best PHP resource
    A good PHP FAQ
    PLEASE remember to wrap your code in [PHP] tags.
    PHP Code:
    // Replace this
    if(isset($_POST['submitButton']))
    // With this
    if(!empty($_POST))
    // Then check for values/forms. Some IE versions don't send the submit button 
    Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live.

  • #3
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    4,290
    Thanks
    23
    Thanked 611 Times in 610 Posts
    This is my code. Adapt it to your circumstances.
    Code:
    <?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 />';
    ?>


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •