I need to count all the records that have this identifierpub-1374077Q
Carry out the following procedure:
$id_pub = "pub-1374077Q";
$stmt=$con->prepare("SELECT id FROM visitors_table where publishers=?");
$stmt->bind_param("s",$id_pub);
$stmt->execute();
$stmt->store_result();
$stmt->bind_result($id);
if ($stmt->num_rows>0) {
return $stmt->affected_rows;
} else {
echo "0 registros";
}
but it does not execute any error, it does not show me anything.
As has already been said, if what you want is to obtain the total of records that meet a certain criteria, you can do it in the following way:
What has been done is the following:
$total
initialized to0
, which will be the one that will collect the value brought from the query. Note that here we do not have to ask if the query returned rows or not. When you do not bring anything,$total
it will remain valid0
.COUNT
in the query to count the total number of records that meet the criteriaexecute
we will usebind_result
to assign the row total to the variable$total
fetch
, since we're looking for a single row/column, itfetch
will hover over that row and$total
get the value we need. That's enough, you don't have to open loopswhile
or anything to read the data. This procedure is explained in the PHP Manual when it says :We are going to change the consultation procedure, making a
SELECT COUNT
general one.So your code would look like this:
:mail
$procesa
accesses$consulta
and finally the methodfetchColumn()
which is much more convenient since as it will use an aggregation function and it will only return a column, it does not use excessive memoryIf you want to consult in depth about fetchColumn, here is the official doc
http://php.net/manual/en/pdostatement.fetchcolumn.php