gpt4 book ai didi

php - 为什么 'SELECT count()' 返回值 'Array' ?

转载 作者:行者123 更新时间:2023-11-29 12:09:45 25 4
gpt4 key购买 nike

我正在尝试向我的 MySQL 结果页面添加分页。我目前的目标是将列中的结果数 ($items_number) 除以 10,因为我希望每页有 10 个结果 ($per_page = 10)。

我必须将 $items 转换为数组,因为它无法作为字符串读取,所以现在我有 $items_number = mysqli_fetch_row($items);

当我在代码中回显此内容以查看列中的项目数时,我收到一条消息:

Notice: Array to string conversion in /home/ikb2014/public_html/test/classic_cars/pag_test.php on line 46

但它仍然返回一个表示“Array”的值。如何获取报告列中项目数而不是“数组”一词的值?

代码:

<?php
require_once("./includes/database_connection.php");

error_reporting(E_ALL);
ini_set('display_errors', 1);

$per_page = 10;
$query = "SELECT productCode, productName, productLine, productScale, productVendor, productDescription, buyPrice FROM products WHERE `productLine` = 'Classic Cars'";
$result = mysqli_query($dbc, $query)
or die(mysqli_error($dbc));

$query_count = "SELECT count(productLine) FROM products WHERE `productLine` = 'Classic Cars'";
$items = mysqli_query($dbc, $query_count)
or die(mysqli_error($dbc));
$items_number = mysqli_fetch_row($items);


?>

<!DOCTYPE html>
<html>
<head>
<meta charset='utf-8'>
<title>Home</title>
<link type="text/css" rel="stylesheet" href="classic_cars.css" />
</head>

<body>
<?php
require_once("./includes/navigation.php");
?>


<?php
while ($row = mysqli_fetch_array($result)) {
$product_code = $row['productCode'];
$product_name = $row['productName'];
$product_line = $row['productLine'];
$product_vendor = $row['productVendor'];
$product_description = $row['productDescription'];
$buy_price = $row['buyPrice'];

echo "<tr>
<td><p>$product_name</p></td>
<td><p>$items_number</p></td>
</tr>";

} // end while ($row = mysqli_fetch_array($result))

?>


<?php
require_once("./includes/footer.php");
?>
</body>
</html>

最佳答案

来自 php 文档:

mysqli_result::fetch_row -- mysqli_fetch_row — Get a result row as an enumerated array

Fetches one row of data from the result set and returns it as an enumerated array, where each column is stored in an array offset starting from 0 (zero)

我将针对您的问题发布 2 个解决方案:首先是使用

mysqli_fetch_row

使用 COUNT 函数(如您的代码中所示):

$row = mysqli_fetch_row($items);
$items_number = $row[0];

第二个是删除 COUNT FUNCTION 并使用

mysqli_num_rows

像这样

$query_count = "SELECT productLine FROM products WHERE `productLine` = 'Classic Cars'";
$items = mysqli_query($dbc, $query_count)
or die(mysqli_error($dbc));
$items_number = mysqli_num_rows($items);

关于php - 为什么 'SELECT count()' 返回值 'Array' ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30900200/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com