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>
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);
I'd do
SELECT count(productLine) as productcount FROM products WHERE `productLine` = 'Classic Cars'
So the key will be productcount
in your array. Then
echo "<tr>
<td><p>$product_name</p></td>
<td><p>" . $items_number['productcount'] . "</p></td>
</tr>";
to display it. That number is always going to be the same though, is that what you want?
Oh, also use mysqli_fetch_assoc
for that. So $items_number = mysqli_fetch_assoc($items);
I think you in your current implementation
echo "<tr>
<td><p>$product_name</p></td>
<td><p>" . $items_number[0] . "</p></td>
</tr>";
would work. I prefer using assigned names though incase a new column is added to the query or removed. Your 0
will always be the first column.
Links:
http://php.net/manual/en/mysqli-result.fetch-row.php
http://php.net/manual/en/mysqli-result.fetch-assoc.php
Try changing this line
SELECT count(productLine) FROM products WHERE `productLine` = 'Classic Cars'
to
SELECT count(*) FROM products WHERE `productLine` = 'Classic Cars'