dongli8722 2015-06-17 19:02
浏览 48
已采纳

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

I am trying to add pagination to my MySQL results page. My goal at the moment is is to divide the number of results in the column ($items_number) by 10 since I want 10 results per page ($per_page = 10).

I had to convert $items into an array since it could not be read as a string, so now I have $items_number = mysqli_fetch_row($items);

When I echo this in my code to see the number of items in a column, I get a message that says:

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

But it also STILL returns a value that says 'Array'. How can I get the value to report the number of items in the column instead of the word 'Array'?

Code:

<?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>
  • 写回答

3条回答 默认 最新

  • douque8861 2015-06-17 19:13
    关注

    From php documentation:

    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)

    I will post 2 solutions for your problem: First is to use

    mysqli_fetch_row

    with COUNT function(like in your code):

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

    Second is to drop the COUNT FUNCTION and use

    mysqli_num_rows

    like this

    $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);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 ogg dd trandata 报错
  • ¥15 高缺失率数据如何选择填充方式
  • ¥50 potsgresql15备份问题
  • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错