A
A
Alexey2019-06-29 15:35:41
PHP
Alexey, 2019-06-29 15:35:41

How to display using PHP - the number of records from the SQL table (base) on the html page?

Dear experts!
I ran into a problem - where I did not expect it at all ..
I need to use PHP to display the number of records from a SQL table (base) on an html page.
I thought it was a trifling matter, but I have been fighting for the 2nd day, and all to no avail ..
What I did and how I reasoned:
First of all, I wrote an SQL query and checked its performance directly in the SQL DBMS:
Here it is:

SELECT COUNT(*) FROM `items_table` WHERE id_category = '1'

I have a quite logical result: COUNT(*) = 5 (records included in the category with id='1')
Query, working and I decide to move on..
Next, I write a function, actually "Getting the number of records (Items)":
PHP:
<i>function get_count_items($id_category){
  global $connect_sql;
  $sql = "SELECT COUNT(*) FROM `my_php_blog`.`items_table` WHERE id_category = $id_category";
  $result = mysqli_query($connect_sql, $sql);
  $count_items_return = mysqli_fetch_all($result, 1);
  return $count_items_return;
}</i>

And I call it on the index.php page:
<?php $id_count_category = 1; ?> // задаю id-нужной категории

<?php $count_items_return = get_count_items($id_count_category); ?>//вызываю функцию с параметром id-категории

And further, what I just didn’t do in order to display this figure (number of records (Items)):
Output options (1):
<i><h4> <?php echo $count_items_return; ?> записей</h4></i>

Result: Array of records
Output options (2):
<i><h4> <?php echo $count_items_return[0]; ?> записей</h4></i>

Result: Array of entries (same)
Here I was hoping to refer to the 1st (0th) element of the array - but also a fiasco..

Output options (3)
( without echo ):
<i><h4> <?php $count_items_return; ?> записей</h4></i>

Result: records (blank instead of numbers)

Output options (4)
(using var_dump):
<i><h4><?php var_dump($count_items_return); ?> записей</h4></i>

Result:
array(1)
  { [0]=>
     array(1)
       { ["COUNT(*)"]=> 
       string(1)
"5" }} записей

Yes, at the end of this output - it really displays the actual number of Records in the desired category.
But how to display just a number (without additional information)??
I also tried on the function side to return not a variable, but an array:
PHP:
function get_count_items($id_category){
  global $connect_sql;
  $sql = "SELECT COUNT(*) FROM `my_php_blog`.`items_table` WHERE id_category = $id_category";
  $result = mysqli_query($connect_sql, $sql);
  $count_items_return = mysqli_fetch_all($result, 1);
  return <b>$count_items_return[0];</b>
}

However, the result is empty: ... records
======================================== ==========
Whether skis do not go, whether that that I do not understand....
How to make an output of a kind: "5 records" on the page?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
E
Evgeny Samsonov, 2019-06-29
@azorr

<?php echo $count_items_return[0]['COUNT(*)']; ?>

K
kolosovas, 2019-07-11
@kolosovas

Try doing AS `count_items`
$sql = "SELECT COUNT(*) AS `count_items` FROM `my_php_blog`.`items_table` WHERE id_category = $id_category";
Then your output will be somewhat different and you can take the value more beautifully
echo $count_items_return[0]['count_items'];
But it seems to me that it would be more elegant to do it through fetch_assoc ();
Good luck.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question