簡體   English   中英

我正在嘗試使用列數創建動態表,但是我無法從sql查詢中獲取列數

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

我正在嘗試創建一個動態表,該表對某個表中的列進行計數,返回該值,然后根據該計數構建一個表。 我下面的代碼沒有任何額外的表行,但是我什至沒有看到這第一條表行。

我收到此錯誤消息:

注意:在第26行的/Applications/XAMPP/xamppfiles/htdocs/database/public_html/includes/class_lib.php中,無法將mysqli_result類的對象轉換為int

這是我的代碼:

    $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;

編輯:對不起,我一開始誤解了您想在這里做什么。

肖恩給了你答案:

// 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

我想到了:

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

我必須除以二,因為數組中的所有信息都是兩倍。

感謝所有的建議!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM