Is there any way to show the result of a query (mysqli) in the form of HTML?

2

I have already looked intensively for a solution to show the result of a mysqli query within an html, but I can not find anything. I saw that it is possible to present the result through one with the help of fetch_assoc () in php, but what I want does not need to be shown inside a table in my perspective. Here is the code of what I want to show, I have already tested it in full php and it shows exactly what I intend, the only obstacle at this moment is to be able to incorporate that same result into an HTML page. I hope you can help me, regards.

     <?php
            require_once('connconf.php'); 

            $conn = mysqli_connect($server, $user, $pw, $bdname) or die ('Connection Error');

            $sqlquery = "select ID_Vote from Votes where ID_Player = '1'"; 

            if($result = mysqli_query($conn, $sqlquery))
                {

                    $rowcount = mysqli_num_rows($result);

                   echo $rowcount;   //this is the value i want to publish on a HTML <label>
                }

        ?>
    
asked by anonymous 26.08.2015 / 23:58

4 answers

4

Full HTML table

If you want to display all the data in a table, regardless of the query , you have this more elaborate solution, which shows the results in a table format,

<?php
   require_once('connconf.php');
   $conn = mysqli_connect($server, $user, $pw, $bdname) or die ( 'Connection Error' );
   $sqlquery = "SELECT * FROM votes";

   $result = mysqli_query($conn, $sqlquery) or die ( mysqli_error( $conn ) );

   $header = true;
   echo '<table>';
   while( $res = mysqli_fetch_assoc( $result ) ) {
      if( $header ) {
         foreach( $res as $campo => $valor ) {
            echo'<th>'.htmlentities( $campo ).'</th>';
         }
         $header = false;
      }
      echo '<tr>';
      foreach( $res as $campo => $valor ) {
         echo'<td>'.htmlentities( $valor ).'</td>';
      }
      echo '</tr>';
   }
   echo '</table>';
?>

This solution adapts to the result of query regardless of the number of columns, and already shows the field names correctly in the table title.

Example:

SELECT * FROM cadastro

Result

 id nome documento  
1 Roberto 129.132.111-33
2 Maria 212.332.718-83


Debug Fast

If it's just a quick debug , with little data this should suffice:

<?php
    require_once('connconf.php'); 
    $conn = mysqli_connect($server, $user, $pw, $bdname) or die ('Connection Error');
    $sqlquery = 'SELECT ID_Vote from Votes where ID_Player = 1'; 

    if( $result = mysqli_query($conn, $sqlquery) ) {
       echo '<pre>';
       print_r( mysqli_fetch_all( $result, MYSQLI_ASSOC ) );
       echo '</pre>';
    }
?>
    
25.12.2015 / 20:37
0

I assume everything is working correctly in your Query , so to display the result of the $rowcount variable on an html page, you can use this form within any .php file between tags HTML :

<label><?=$rowcount?></label>
    
27.08.2015 / 01:29
0

Another possibility is to embed the html in the following way.

Echo" <label>.$rowcount.</label>";
    
27.08.2015 / 02:49
0

Suppose you are doing this query are in the same file that you want to show the result in the label, let's call it just for the sake of example showing result.php

Show result.php

     <?php
            require_once('connconf.php'); 

            $conn = mysqli_connect($server, $user, $pw, $bdname) or die ('Connection Error');

            $sqlquery = "select ID_Vote from Votes where ID_Player = '1'"; 

            if($result = mysqli_query($conn, $sqlquery))
                {

                    $rowcount = mysqli_num_rows($result);
?>


                   <label><?php echo $rowcount;?></label>



<?php
                }

        ?>

The explanation is simple, you open and close the php tag whenever you want to use html.

    
25.12.2015 / 13:20