A
A
animr2016-01-06 21:34:16
PHP
animr, 2016-01-06 21:34:16

How to get the number of records in the database?

People tell me why these errors come out:
1. mysqli_fetch_assoc() expects parameter 1 to be mysqli_result, boolean given in
2. mysql_num_rows() expects parameter 1 to be resource, object given in
Connected to the database, The query is as follows:
$query = "SELECT COUNT (*) FROM table WHERE p_Category = 'Down jacket'";
function to determine the number of matching entries:
function get_CountPName($category_name) {
global $connection;
$query = "SELECT COUNT(*) FROM table WHERE p_Category = $category_name";
$res = mysqli_query($connection, $query);
return mysqli_fetch_assoc($res);
}
I call the function as follows:
$CountRec = get_CountPName('Puffcoat');
what is wrong in the code?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Sergey Novikov, 2016-01-06
@BOOMER_74

Write:
However, there are no quotes in the query. Maybe so:

function ...
$query = "SELECT COUNT(*) FROM table WHERE p_Category = '$category_name'";
...
$CountRec = get_CountPName('Пуховик');

O
Oleg Pavlov, 2016-01-06
@onpavlov

Here "mysqli_fetch_assoc() expects parameter 1 to be mysqli_result, boolean given in" you are told that in parameter 1 the function expects the mysqli_result type and a boolean value has arrived.
Look at what request is being created, go to phpmyadmin or somewhere else and experiment yourself, achieve the desired result so that a value is returned on request, you most likely have false in $res

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question