简体   繁体   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

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 注意:在第26行的/Applications/XAMPP/xamppfiles/htdocs/database/public_html/includes/class_lib.php中,无法将mysqli_result类的对象转换为int

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 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! 感谢所有的建议!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我可以使用SQL同时计算行数和获取列值吗? - Can I count rows and get a column value at the same time with SQL? 我收到此错误,有人可以帮忙吗? 列数与第1行的值数不匹配 - I get this error, can someone help? Column count doesn't match value count at row 1 我正在尝试使用表格中的数据填充模式文本框,但无法正常工作 - I'm trying to populate my modal textboxes with datas from my table but i can't get it to work 在PHP中,如何获取mySQL表列中每个实例的计数? - In PHP how can I get the count of each instance in a mySQL table column? SQL COUNT表的一列 - SQL COUNT one column of table 如何在PHP中从mysql查询中计数数组值并将第二列与计数结果相关联 - How can I count array values from mysql query in PHP and associate a second column with the count results PHP / SQL使用“错误:列数与行1的值计数不匹配”形式插入数据库,但无法弄清计数不匹配的原因 - PHP/SQL Inserting to DB using form “Error: Column count doesn't match value count at row 1” but can't figure out why count doesn't match 如何在My SQL的给定行中获取空值列计数 - How to get a empty value column count in a given row in My SQL 我正在尝试将MySQL数据库中的数据显示到HTML表上,但未注册列字段 - I'm trying to display data from a MySQL database onto an HTML table but doesn't register the column fields SQL查询错误:列计数与第1行的值计数不匹配 - SQL Query Error: Column count doesn't match value count at row 1
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM