简体   繁体   中英

I'm trying make a dynamic table with a column count, but I can't get the column count out of my sql query

I'm trying to make a dynamic table that counts the columns in a certain table, returns this value, and then builds a table according to this count. My code below is without the any extra table rows, but I don't even get this first table row to appear.

I get this error message:

Notice: Object of class mysqli_result could not be converted to int in /Applications/XAMPP/xamppfiles/htdocs/database/public_html/includes/class_lib.php on line 26

Here is my code:

    $query = "SELECT COUNT(*) AS Columns 
            FROM INFORMATION_SCHEMA.COLUMNS 
            WHERE table_schema = 'user_info' 
            AND table_name = 'task'";
        $colcnt = $mysqli->query($query);

        $table = "<table><tr>";
        $countcolumn = 1;
        while ($countcolumn <= $colcnt) {
            $table .= "<td class=table_head>";
            $table .= "<a href=?sort=>";
            $table .= "test</a></td>";
            $countcolumn++;
        }
        $table .= "</tr>";
        $table .= "</table>";
        echo $table;

Edit: Sorry I misunderstood what you were trying to do here at first.

Sean has given you the answer:

// Run the query you have provided
$result = $mysqli->query($query);

// Fetch the results as an associative array
$row = mysqli_fetch_assoc($result);

// Get the row named Columns
$colcnt = $row['Columns'];

http://php.net/manual/en/mysqli-result.fetch-assoc.php

I figured it out:

    $query = "DESCRIBE $tablename";
    $result = $this->mysqli->query($query);
    $colcnt = (count(mysqli_fetch_array($result)) / 2);

I have to divide by two cause all the information in the array is double.

Thanks for all the suggestions!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM