#1
  1. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jan 2017
    Posts
    664
    Rep Power
    0

    How To Count Mysql table Column Numbers ?


    Hi,

    Can someone be kind enough to show me how to implement this following formula to count the mysql table column numbers ?
    int mysqli_stmt_field_count ( mysqli_stmt $stmt )

    I need to see how to count the column numbers and echo it.
    PHP: mysqli_stmt::$field_count - Manual


    Here's my rough attempt. Note that, the php manual is not showing any code sample and so I'm totally kicking in the dark wherever I can.
    Note the commented out lines to get an idea how I been experimenting before I came to this final code.

    Here is the code I found somewhere. Seems like half done:
    PHP Code:
    if (mysqli_stmt_field_count($stmt)) {
        
    /* this was a select/show or describe query */
        
    $result mysqli_stmt_store_result($stmt);

        
    /* process result set */
        
    $row mysqli_stmt_fetch_row($result);

        
    /* free result set */
        
    mysqli_free_result($result);

        
    /* close connection */
        
    mysqli_close($conn); 
    Anyway, I tried filling in the blanks but at a total loss. I would be since the php manual showed no sample code. Nor google pulls out any.
    PHP Code:
    <?php 

    //Required PHP Files.
    include 'config.php';
    include 
    'header.php';
    include 
    'account_header.php';

    //Check if User is already logged-in or not. Get the login_check() FUNCTION to check. 
    if (login_check() === FALSE)
    {
        
    //Redirect User to Log-in Page immediately.
        //header("refresh:0; url=login.php");
        
    header("location:login.php");
        exit();
    }
    else
    {
        
    $user $_SESSION["user"];
        
        
    $query "";
        
    $stmt mysqli_prepare($conn$query);     
        
    //mysqli_stmt_bind_param($stmt,'ss',$recipient_username,$sender_username); 
        
    mysqli_stmt_execute($stmt);  
        
    //$result_1 = mysqli_stmt_bind_result($stmt_1,$matching_rows_count); 
        //mysqli_stmt_fetch($stmt_1);
        //mysqli_stmt_free_result($stmt_1); 
        
        
    $fields_count mysqli_stmt_field_count($stmt);
        if (
    mysqli_stmt_field_count($stmt)) {
        
    /* this was a select/show or describe query */
        
    $result mysqli_stmt_store_result($stmt);

        
    /* process result set */
        
    $row mysqli_stmt_fetch_row($result);

        
    /* free result set */
        
    mysqli_free_result($result);

        
    /* close connection */
        
    mysqli_close($conn);

            echo 
    "Total Table Column Count: $fields_count<br>";
        
    printf(" %d fields found.\n",$fields_count); 
    }
    }
    ?>
    Note my blank: $query =.
    I need someone to show me how to do the query here.
    Last edited by UniqueIdeaMan; April 4th, 2018 at 01:47 PM.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jan 2017
    Posts
    664
    Rep Power
    0
    Any ideas ?
  4. #3
  5. Banned (not really)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Dec 1999
    Location
    Caro, Michigan
    Posts
    14,921
    Rep Power
    4554
    What do you think "field" means in this context? Why do you think you need to know it?

    For example, if your query is "SELECT firstname, lastname FROM table", then the number of fields is 2: firstname & lastname. It has nothing to do with how the query executed or what the results are. It's just telling you that there'll be two fields in the result, if the query succeeds.

    The only time you'd really need to do this is if you're building queries dynamically and don't know how many fields were added to the dynamic query and don't have another means to figure that out.
    -- Cigars, whiskey and wild, wild women. --
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jan 2017
    Posts
    664
    Rep Power
    0
    Originally Posted by Sepodati
    What do you think "field" means in this context? Why do you think you need to know it?

    For example, if your query is "SELECT firstname, lastname FROM table", then the number of fields is 2: firstname & lastname. It has nothing to do with how the query executed or what the results are. It's just telling you that there'll be two fields in the result, if the query succeeds.

    The only time you'd really need to do this is if you're building queries dynamically and don't know how many fields were added to the dynamic query and don't have another means to figure that out.
    Thanks man! But I knew all this. And no, you haven't wasted your time because future newbies would learn from you the easy way rather than like me googling and learning.
    Look at my query. It is empty. It mentions no column name. I know field means mysql tbl column here. Because, I won't know how many columns there are (like you say about the dynamic stuff), I did not insert any column names.
    As for the bind_param and bind_result, you might be wondering then why have I mentioned the column names there then ? Well, I haven't. They are commended-out, remember ? I just left those lines there as I copied & pasted from another file (working over another file's skeleton).
    Now, if you don't mind, do you mind showing me how to do that field number acquiring query ?
    Better, write 2 queries if needed or 1 where the number of fields is grabbed and then queried for the field names and them grabbed too. And all using PREP STMT and I would appreciate it double if you show how to do it without PREP STMT too. That way, we newbies (or half newbies like me ) learn both ways.
    Yeah, I know. You would reply:
    "I mind!".
    But let's see, if you can prove me wrong.

    Anyone else like Kicken, Catacaustic, Benanamen, Barand, Dsmadismad (sorry, I keep forgetting your Username!), etc. anyone else are welcome to respond promptly.

    Cheers!

    PS - It is 1am here. Been working from 8pm.
    Got to sleep.
    Good night everyone (especially Sepo!).
    Last edited by UniqueIdeaMan; April 8th, 2018 at 02:05 PM.
  8. #5
  9. Banned (not really)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Dec 1999
    Location
    Caro, Michigan
    Posts
    14,921
    Rep Power
    4554
    But I knew all this.
    Based on your questions/demands at the end of your post, no you don't.
    -- Cigars, whiskey and wild, wild women. --
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jan 2017
    Posts
    664
    Rep Power
    0
    Originally Posted by Sepodati
    Based on your questions/demands at the end of your post, no you don't.
    What I meant was: I knew the "field count" meant the "columns count" of the mysql table.
    Now, based on my questions, any sane man or woman would say I don't know how to make the query to get the "field count" and then the "fields names".
    Now, that was not too hard to understand or figure-out. Now was it ? Nevermind! When are you going to understand me ? How long will it take ? Oh Nevermind! Don't bother answering these. (It's obvious, you have a communication problem with me for some unknown reason even though I am speaking plain English). But do bother to answer how I should make the queries I mentioned earlier.

    Cheers!

IMN logo majestic logo threadwatch logo seochat tools logo