Insert data from a Postgresql Query to an HTML table

1

Good afternoon, I'm trying to make a table in HTML using PHP to establish communication with the database, the query is as follows

$query1="SELECT b.nombreforense, b.apelldoforense, COUNT(*) AS Total 
          FROM fallecido AS a 
    INNER JOIN forense AS b ON(a.forense_idforense = b.idforense) 
      GROUP BY b.nombreforense,b.apelldoforense ORDER BY Total";

The code used in php is as follows:

<body>
    <?php
        if(!$records1)
        {    
        echo"Existe algun error. \n";
        exit;
        }

    echo "<table border=1px solid black>";
        echo "<th>Nombre y Apellido Forense </th><th>Casos Atendidos</th>";
        while ($row1=pg_fetch_array($records1))
        {   

            echo "<tr>";
                echo "<td align='center'>".$row1['nombreforense']." ".$row1['apelldoforense']."</td>"."<td>".$row1[Total]."</td>";
            echo"</tr>";
        }
    echo "</table>";
    ?>


</body>

The problem is that if you bring the data of the attributes "nombreforense" and "apelldoforense" the data of the total that is obtained through the COUNT (*) does not do what it shows is the following

I need to be able to show the cases attended and I do not know what the error is or what is missing in the code

    
asked by Carlos Alberto Pedron 11.04.2017 в 21:38
source

4 answers

0

I would try to rename the Total column, in lowercase, both in the query and in the PHP code.

Postgresql does not distinguish between upper and lower case in column names.

    
answered by 12.04.2017 / 22:12
source
2

In postgres, in order to recognize the capital letters, you have to put quotation marks

SELECT b.nombreforense, b.apelldoforense, COUNT(*) AS "Total" 
FROM fallecido AS a 
INNER JOIN forense AS b ON(a.forense_idforense = b.idforense) 
GROUP BY b.nombreforense,b.apelldoforense ORDER BY Total

But, it recognizes it as a whole in lowercase.

Bone $ row1 ['total'] should work.

In these cases I like to make a print_r ($ row1) so that PHP itself renders the whole object. There you will see with what KEY you can refer to the value you are looking for.

Rod

    
answered by 13.04.2017 в 17:03
1

Missing the quotes in Total

echo "<td align='center'>".$row1['nombreforense']." ".$row1['apelldoforense']."</td>"."<td>".$row1['Total']."</td>";
    
answered by 11.04.2017 в 22:40
0

If your query generates the data, then try to separate the columns of your table, I mean something like this ...

echo "<td align='center'>".$row1['nombreforense']." ".$row1['apelldoforense']."</td>";

echo "<td>".$row1['Total']."</td>";

It may be assuming the same string of characters in the echo and does not recognize your data in the second column, so the whole echo equals one. It will also be a more understandable code for any programmer who edits the code later

Greetings

    
answered by 12.04.2017 в 18:00