How do I fix it? – PHP – SitePoint Forums

I have three rows but it only shows one row in report table
what is the error

source code




                

prepare("SELECT name as n, birth as b, Cost as c, count(id) as total FROM patient WHERE fixation_t=:fix or type_of_c=:type_of_c"); //$stm = $db->prepare("SELECT * FROM sub WHERE cl=:class or gr=:gr or sn=:sn or (datee between :fdate and :tday AND cl=:class) "); $stm->bindParam(":fix",$fix, PDO::PARAM_STR); $stm->bindParam(":type_of_c",$type_of_c, PDO::PARAM_STR); //$stm->bindParam(":sn",$sn, PDO::PARAM_STR); //$stm->bindParam(":fdate",$fdate, PDO::PARAM_STR); //$stm->bindParam(":tday",$tday, PDO::PARAM_STR); $stm->execute(); $rowCount = $stm->rowCount(); if($rowCount > 0){ ?>
patient Report from to
fetchAll(PDO::FETCH_ASSOC); $cnt=1; foreach ($results as $row) { ?>
# Name birth Fixation Type Doctor No:Impalnt Implant System Cost Action
Qty '.$row['Impalnt_n'].''; ?> $ " class="btn btn-warning btn-sm">
Total Cost
count


This is because you are using a COUNT() aggregate function. This will consolidate the result set into a single row.

What exactly are you trying to count in this query?

Lines The number of lines as shown below

So… let’s take a moment to look at what you have.

You select 4 elements: n, b, c and total.

Here you expect $row to contain id, n, b, fixation_t, doctor, Implant_n, typeand c.

There’s… a lot of stuff missing from your request, don’t you think?

If there is no data in the table, show msg there is no data

It still hasn’t been fixed after you noticed it

Show us your updated code.

updated




                

prepare("SELECT * FROM patient WHERE fixation_t=:fix or type_of_c=:type_of_c or Size=:s "); //$stm = $db->prepare("SELECT * FROM sub WHERE cl=:class or gr=:gr or sn=:sn or (datee between :fdate and :tday AND cl=:class) "); $stm->bindParam(":fix",$fix, PDO::PARAM_STR); $stm->bindParam(":type_of_c",$type_of_c, PDO::PARAM_STR); $stm->bindParam(":s",$s, PDO::PARAM_STR); //$stm->bindParam(":sn",$sn, PDO::PARAM_STR); //$stm->bindParam(":fdate",$fdate, PDO::PARAM_STR); //$stm->bindParam(":tday",$tday, PDO::PARAM_STR); $stm->execute(); $rowCount = $stm->rowCount(); if($rowCount > 0){ ?>
patient Report from to
fetchAll(PDO::FETCH_ASSOC); $cnt=1; foreach ($results as $row) { ?>
# Name birth Fixation Type Doctor No:Impalnt Implant System Cost Action
Qty '.$row['Impalnt_n'].''; ?> $ " class="btn btn-warning btn-sm">
Total Cost
count

I don’t know what the actual problem is now, but you can’t use $row['total'] in your table footer – $row does not exist outside your foreach() loop. Also you add $totalmarks to a running total, but that variable doesn’t seem to be created anywhere.

Is the problem still the same, only showing one line? Does the query return all rows when you test it in phpmyadmin?

Undefined variable error: $totlcount
Undefined variable error: $totalmarks

I will assume from the previous query that the database table does not contain a column totaland even if it was, the foreach closed, so

Undefined variable error: $row[‘total’];

Logical error:

Your query does not make any limitation based on these date values.




                

prepare("SELECT id as idi, name as n, registration_d as r, doctor as d, Cost as c, count(id) as total FROM patient WHERE fixation_t=:ff || type_of_c=:tyy || Size=:ss"); $stm->bindParam(":ff",$ff, PDO::PARAM_STR); $stm->bindParam(":tyy",$tyy, PDO::PARAM_STR); $stm->bindParam(":ss",$ss, PDO::PARAM_STR); //$stm->bindParam(":fdate",$fdate, PDO::PARAM_STR); //$stm->bindParam(":tday",$tday, PDO::PARAM_STR); //$stm->bindParam(":sn",$sn, PDO::PARAM_STR); $stm->execute(); $rowCount = $stm->rowCount(); if($rowCount > 0){ ?>
patient Report from to
fetchAll(PDO::FETCH_ASSOC); $cnt=1; foreach ($results as $row) { ?>
# Name re Doctor Cost Action
$ " class="btn btn-warning btn-sm">
Total Cost
count

Yes it only shows one line

You put the count() return to the query.

Do not use your query to count or total items if you want to display the line item information.

Pull the line item information with your query and use PHP to total and count.



1 like

You already have an example in your code.

First, set your count to start at 0 before the foreach.
Then, with each loop of the foreach, add the cost of that item to the total.
At the end of the loop, the total will have the total value of all items.

I did and it wasn’t fixed

You already have a count of the number of rows in the result set. It’s in $rowCount. Just echo it instead of $row[‘total’] (which exists after the end of a foreach(){} loop if count() as total in the query turned out to be the solution, which it is not.)



1 like

James S. Joseph