[英]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.